![]() ![]() Run the following command to generate EF Core migrations for SQL Server and store them in their own folder. dotnet ef migrations add InitialCreate -context SqliteDataContext -output-dir Migrations/SqliteMigrations Run the following command to generate EF Core migrations for SQLite and store them in their own folder. Options.UseSqlite(Configuration.GetConnectionString("WebApiDatabase")) ![]() Public SqliteDataContext(IConfiguration configuration) : base(configuration) Public class SqliteDataContext : DataContext Having a second EF Core DB Context that derives from the main DB context is what enables the project to support multiple different database providers. Options.UseSqlServer(Configuration.GetConnectionString("WebApiDatabase")) Ĭreate Development EF Core DB Context for SQLiteĬreate a development DB context that inherits from the main DB context above and overrides the database provider in the OnConfiguring() method.īelow is the development DB context from the example ASP.NET Core api that overrides the database provider to connect to SQLite instead of SQL Server. Protected override void OnConfiguring(DbContextOptionsBuilder options) Public DataContext(IConfiguration configuration) ![]() Protected readonly IConfiguration Configuration Cannot use sqlitemanager how to#The below steps show how to use a SQLite database in development and a SQL Server database in production, but you could switch these to any database providers you like that are supported by EF Core.Ĭreate Main EF Core DB Context for SQL ServerĬreate the main DB context class that defines the entities available in the database via public DbSet properties, and configure it to connect to the production database (SQL Server in this case).īelow is the main DB context from the example ASP.NET Core api linked above, it has the class name DataContext and is located in the /Helpers directory of the project, but you can choose any class name and directory you prefer. Cannot use sqlitemanager code#The code snippets in this post are taken from this example ASP.NET Core API which I just finished updating to support multiple databases for different environments, specifically SQLite in development and SQL Server in production. In this post we'll go through an example of how to setup an ASP.NET Core project with EF Core DB Contexts and Migrations that support multiple different database providers. Example code tested with ASP.NET Core 3.1 ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |