【问题标题】:Receiving weird host can't be null error with ef coreef core 接收奇怪的主机不能为空错误
【发布时间】:2020-11-19 16:50:33
【问题描述】:

我最近使用 postgresql 在一个家庭项目上安装了 EF Core 3.1.6。

我有连接字符串,并且可以使用包管理器控制台轻松添加迁移。如果我运行我的应用程序,Database.Migrate() 将轻松迁移迁移。但是,如果我尝试使用“更新数据库”通过包管理器控制台更新数据库,则会收到此错误:

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

我不知道发生了什么,如果我缺少一个 nuget 包等。任何可能的帮助和解释将不胜感激。

【问题讨论】:

    标签: ef-core-3.1


    【解决方案1】:

    我解决了。它与我在运行时而不是构建时构建连接字符串的方式有关。

    【讨论】:

    • 你是怎么解决的?您是否在运行时找到了解决方案?我正在使用环境变量,并且必须在 powershell 中设置它们才能使其工作。不知何故,它不会记录默认值...
    【解决方案2】:

    您应该先从终端导出您的环境变量,如下所示:

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

    {dev.env} -> 将其更改为您的 env 文件

    【讨论】:

      猜你喜欢
      • 2021-11-18
      • 1970-01-01
      • 2019-05-13
      • 1970-01-01
      • 2012-12-28
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 2016-11-30
      相关资源
      最近更新 更多