不支持关键字:' host'在POSTGRESQL上迁移时出错

时间:2017-10-07 03:44:42

标签: asp.net-mvc postgresql .net-core database-migration

目前我正在使用.netcore 2.0上的MVC6进行数据库迁移。这是我要关注的链接,用于迁移POSTGRESQL:https://damienbod.com/2016/01/11/asp-net-5-with-postgresql-and-entity-framework-7/

在我的appsettings.json中:

    {
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "ConnectionStrings": {
    "DataAccessPostgreSqlProvider": "User ID=ids;Password=****;Host=127.0.0.1;Port=5432;Database=IDS;Pooling=true;"
  }
}

我使用以下命令开始迁移:

dotnet ef migrations add testPG

它成功并且没有任何错误,但是当我尝试在命令提示符下运行以下命令时,错误就出现了。

dotnet ef database update

这是我在cmd上看到的错误。

enter image description here

有人可以告诉我这个吗?

编辑完整的堆栈跟踪在这里:

    info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using 'C:\Users\Richard\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
System.ArgumentException: Keyword not supported: 'hostaddr'.
   at System.Data.Common.DbConnectionOptions.ParseInternal(Dictionary`2 parsetable, String connectionString, Boolean buildChain, Dictionary`2 synonyms)
   at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Dictionary`2 synonyms)
   at System.Data.SqlClient.SqlConnectionString..ctor(String connectionString)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions)
   at System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key)
   at System.Data.SqlClient.SqlConnection.set_ConnectionString(String value)
   at System.Data.SqlClient.SqlConnection..ctor(String connectionString)
   at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerConnection.CreateDbConnection()
   at Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.get_DbConnection()
   at Microsoft.EntityFrameworkCore.Internal.RelationalLoggerExtensions.MigrateUsingConnection(IDiagnosticsLogger`1 diagnostics, IMigrator migrator, IRelationalConnection connection)
   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.UpdateDatabase.<>c__DisplayClass0_1.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Keyword not supported: 'hostaddr'.

谢谢你 青蛙

1 个答案:

答案 0 :(得分:2)

确保实现IDesignTimeDbContextFactory的类包含PostgreSQL的正确数据库定义。请注意此行builder.UseNpgsql(connectionString);

public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<DomainModelPostgreSqlContext>
{
    public DomainModelPostgreSqlContext CreateDbContext(string[] args)
    {
        IConfigurationRoot configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .Build();
        var builder = new DbContextOptionsBuilder<DomainModelPostgreSqlContext>();
        var connectionString = configuration.GetConnectionString("DataAccessPostgreSqlProvider");
        builder.UseNpgsql(connectionString);
        return new DomainModelPostgreSqlContext(builder.Options);
    }
}