You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: entity-framework/core/managing-schemas/migrations/providers.md
+35-36
Original file line number
Diff line number
Diff line change
@@ -2,48 +2,39 @@
2
2
title: Migrations with Multiple Providers - EF Core
3
3
description: Using migrations to manage database schemas when targeting multiple database providers with Entity Framework Core
4
4
author: bricelam
5
-
ms.date: 11/08/2017
5
+
ms.date: 10/29/2020
6
6
uid: core/managing-schemas/migrations/providers
7
7
---
8
8
# Migrations with Multiple Providers
9
9
10
-
The [EF Core Tools][1] 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. There are two ways to handle this with Migrations. You can maintain two sets of migrations--one for each provider--or merge them into a single set
11
-
that can work on both.
10
+
The [EF Core Tools](xref:core/miscellaneous/cli/index) 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.
12
11
13
-
## Two migration sets
12
+
## Using multiple context types
14
13
15
-
In the first approach, you generate two migrations for each model change.
16
-
17
-
One way to do this is to put each migration set [in a separate assembly][2] and manually switch the active provider (and migrations assembly) between adding the two migrations.
18
-
19
-
Another approach that makes working with the tools easier is to create a new type that derives from your DbContext and overrides the active provider. This type is used at design time when adding or applying migrations.
14
+
One way to create multiple migration sets is to use one DbContext type per provider.
> You don't need to specify the output directory for subsequent migrations since they are created as siblings to the
53
44
> last one.
54
45
55
-
## One migration set
46
+
## Using one context type
56
47
57
-
If you don't like having two sets of migrations, you can manually combine them into a single set that can be applied to both providers.
48
+
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](xref:core/managing-schemas/migrations/projects) for instructions on setting up your projects.
58
49
59
-
Annotations can coexist since a provider ignores any annotations that it doesn't understand. For example, a primary key column that works with both Microsoft SQL Server and SQLite might look like this.
50
+
Starting in EF Core 5.0, 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.
60
51
61
-
```csharp
62
-
Id=table.Column<int>(nullable: false)
63
-
.Annotation("SqlServer:ValueGenerationStrategy",
64
-
SqlServerValueGenerationStrategy.IdentityColumn)
65
-
.Annotation("Sqlite:Autoincrement", true),
52
+
Here's one pattern that works well when using a [Generic Host](/dotnet/core/extensions/generic-host).
Since the default host builder reads configuration from command-line arguments, you can specify the provider when running the tools.
57
+
58
+
### [.NET Core CLI](#tab/dotnet-core-cli)
59
+
60
+
```dotnetcli
61
+
dotnet ef migrations add MyMigration --project ../SqlServerMigrations -- --provider SqlServer
62
+
dotnet ef migrations add MyMigration --project ../SqliteMigrations -- --provider Sqlite
66
63
```
67
64
68
-
If operations can be applied only for one provider, or they're different between providers, use the `ActiveProvider` property to determine which provider is active:
65
+
> [!TIP]
66
+
> 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.
69
67
70
-
```csharp
71
-
if (migrationBuilder.ActiveProvider=="Microsoft.EntityFrameworkCore.SqlServer")
> The ability to specify additional arguments for the app was added in EF Core 5.0. If you're using an older version, specify configuration values with environment variables instead.
0 commit comments