Receiving weird host can't be null error with ef core

Viewed 4262

I have recently installed the EF Core 3.1.6 on a home project using postgresql.

I have the connection string, and can add migrations easily with the Package Manager Console. If I run my application the Database.Migrate() will migrate the migrations easily. However if I attempt to update the database via the Package Manager Console with an 'update-database' I receive this error:

System.ArgumentException: Host can't be null
   at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlConnection.<>c__DisplayClass32_0.<<Open>g__OpenLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlConnection.Open()
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Host can't be null

I do not know what is happening, if I am missing a nuget package, etc. Any possible help and explanation for what is happening would be greatly appreciated.

2 Answers

I Solved it. It had to do with the way I was building the connection string on runtime and not buildtime.

You should export your env variables first from terminal like this:

export $(grep -v '^#' ./dev.env | xargs)

{dev.env} -> change it to your env file

Related