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)?
I'm working with postgreSQL just like you are saying.
I use this 2 nugget packages:
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:
Then everything work fine. I hope that this can help you :)