.NET Core 2.1 Npgsql.EntityFrameworkCore.PostgreSQL Code-First

.net-core asp.net-core-2.1 entity-framework-6 entity-framework-core postgresql

Question

I create a project in which I would like to connect to an SQLite provider for testing purposes, and use PostgreSQL on production.

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
}

I would like to use the code-first strategy. For SQLite - no problem:

dotnet ef migrations add InitialCreate
dotnet ef database update

In conclusion, is there a similar solution for PostgeSQL (OpenSource)?

1
1
10/18/2018 12:34:55 PM

Popular Answer

I'm working with postgreSQL just like you are saying.

I use this 2 nugget packages:

  • Npgsql;
  • Npgsql.EntityFrameworkCore.PostgreSQL

And I just add this to your Startup.cs -> ConfigureServices:

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

And this to appsettings.json

"ConnectionStrings": {

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

And then I just add a Migration and Update the DB:

  1. dotnet ef migrations add MigrationTest
  2. dotnet ef database update

Then everything work fine. I hope that this can help you :)

0
6/1/2019 5:08:16 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow