【问题标题】:.NET Core 2.1 Npgsql.EntityFrameworkCore.PostgreSQL Code-First.NET Core 2.1 Npgsql.EntityFrameworkCore.PostgreSQL 代码优先
【发布时间】:2019-03-23 06:31:35
【问题描述】:

我创建了一个项目,我想在其中连接到 SQLite 提供程序以进行测试,并在生产环境中使用 PostgreSQL。

protected override void OnConfiguring(DbContextOptionsBuilder opt)
{
 #if DEBUG
  opt.UseSqlite("Data Source=blogging.db");
 #endif
 #if !DEBUG     
  opt.UseNpgsql("Host=localhost;Database=xxxx;Username=xxxxx;Password=xxxxx");
 #endif
}

我想使用代码优先策略。对于 SQLite - 没问题:

dotnet ef migrations add InitialCreate
dotnet ef database update

总之,PostgeSQL(开源)有类似的解决方案吗?

【问题讨论】:

    标签: postgresql .net-core entity-framework-6 entity-framework-core asp.net-core-2.1


    【解决方案1】:

    正如你所说,我正在使用 postgreSQL。

    我使用这 2 个金块包:

    • Npgsql;
    • Npgsql.EntityFrameworkCore.PostgreSQL

    我只是将它添加到您的 Startup.cs -> ConfigureServices:

    options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));
    

    这个到 appsettings.json

    "ConnectionStrings": {
    
           "DefaultConnection": "Server=localhost;Port=5432;Database=postgresTest;User Id=postgres;Password=xyz;"
     }
    

    然后我只需添加一个迁移和更新数据库:

    1. dotnet ef 迁移添加 MigrationTest
    2. dotnet ef 数据库更新

    然后一切正常。我希望这可以帮助你:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-02-15
      • 2021-01-08
      • 1970-01-01
      • 2020-04-29
      • 2019-10-23
      • 1970-01-01
      相关资源
      最近更新 更多