Training
Module
Use a database with minimal API, Entity Framework Core, and ASP.NET Core - Training
Learn how to add a database to a minimal API application.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
The EF Core Tools only scaffold migrations for the active provider. Sometimes, however, you may want to use more than one provider (for example Microsoft SQL Server and SQLite) with your DbContext. Handle this by maintaining multiple sets of migrations--one for each provider--and adding a migration to each for every model change.
One way to create multiple migration sets is to use one DbContext type per provider.
class SqliteBlogContext : BlogContext
{
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite("Data Source=my.db");
}
Specify the context type when adding new migrations.
dotnet ef migrations add InitialCreate --context BlogContext --output-dir Migrations/SqlServerMigrations
dotnet ef migrations add InitialCreate --context SqliteBlogContext --output-dir Migrations/SqliteMigrations
Tip
You don't need to specify the output directory for subsequent migrations since they are created as siblings to the last one.
It's also possible to use one DbContext type. This currently requires moving the migrations into a separate assembly. Please refer to Using a Separate Migrations Project for instructions on setting up your projects.
Tip
You can view this article's sample on GitHub.
You can pass arguments into the app from the tools. This can enable a more streamlined workflow that avoids having to make manual changes to the project when running the tools.
Here's one pattern that works well when using a Generic Host.
public static IHostBuilder CreateHostBuilder(string[] args)
=> Host.CreateDefaultBuilder(args)
.ConfigureServices(
(hostContext, services) =>
{
services.AddHostedService<Worker>();
// Set the active provider via configuration
var configuration = hostContext.Configuration;
var provider = configuration.GetValue("Provider", "SqlServer");
services.AddDbContext<BlogContext>(
options => _ = provider switch
{
"Sqlite" => options.UseSqlite(
configuration.GetConnectionString("SqliteConnection"),
x => x.MigrationsAssembly("SqliteMigrations")),
"SqlServer" => options.UseSqlServer(
configuration.GetConnectionString("SqlServerConnection"),
x => x.MigrationsAssembly("SqlServerMigrations")),
_ => throw new Exception($"Unsupported provider: {provider}")
});
});
Since the default host builder reads configuration from command-line arguments, you can specify the provider when running the tools.
dotnet ef migrations add MyMigration --project ../SqlServerMigrations -- --provider SqlServer
dotnet ef migrations add MyMigration --project ../SqliteMigrations -- --provider Sqlite
Tip
The --
token directs dotnet ef
to treat everything that follows as an argument and not try to parse them as options. Any extra arguments not used by dotnet ef
are forwarded to the app.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Training
Module
Use a database with minimal API, Entity Framework Core, and ASP.NET Core - Training
Learn how to add a database to a minimal API application.
Documentation
Using a Separate Migrations Project - EF Core
Using a separate migration project for managing database schemas with Entity Framework Core
Custom Migrations History Table - EF Core
Customizing a history table to use for migrations with Entity Framework Core
Custom Migrations Operations - EF Core
Managing custom and raw SQL migrations for database schema management with Entity Framework Core