Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

General way to switch between Sqlite or SqlServer in EF Core?

I want to be able to switch on the fly between using SQLite and SqlServer as a db provider for Entity Framework in an ASP.NET Core app.

The usual pattern to associate a DbContext with a Database Provider is via code in the ConfigureServices method:

        services.AddDbContext<FeedbackDbContext>(options => options.UseSqlite(Configuration.GetConnectionString("Feedback")));

Each of the database providers for EF Core adds its own extension method as Use<Provider>.

It is surprising to me that I can't find a way to specify the database provider for a connection string in config

like image 494
Chris Becke Avatar asked Aug 01 '26 12:08

Chris Becke


1 Answers

Very nice article that shows how do have two contexts, one per DbProvider, and to be able to also generate different migrations:

https://jasonwatmore.com/post/2020/01/03/aspnet-core-ef-core-migrations-for-multiple-databases-sqlite-and-sql-server

like image 179
CodeClimber Avatar answered Aug 03 '26 03:08

CodeClimber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!