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: pages/efmigrations.md
+23-2Lines changed: 23 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ To change this file edit the source file and then run MarkdownSnippets.
7
7
8
8
# EntityFramework Migrations
9
9
10
-
[EntityFramework Migrations](https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/) are supported.
10
+
[EntityFramework Migrations](https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/) are supported via the `buildTemplate` parameter on `SqlInstance`. When using migrations, pass the `TemplateFromConnection` overload of `buildTemplate` so that the template database schema is created by applying migrations rather than `EnsureCreatedAsync`.
11
11
12
12
<!-- snippet: Migrations -->
13
13
<aid='snippet-Migrations'></a>
@@ -29,10 +29,27 @@ var sqlInstance = new SqlInstance<MyDbContext>(
29
29
30
30
The above performs the following actions:
31
31
32
+
* Creates a `SqlInstance` using the connection-based `buildTemplate` delegate.
33
+
* Optionally replaces `IMigrationsSqlGenerator` with a custom implementation.
34
+
* Constructs a `DbContext` from the provided `DbContextOptionsBuilder`.
35
+
* Applies all pending migrations via `Database.MigrateAsync()`.
36
+
37
+
The template database is built once and then cloned for each test, so migrations only run a single time regardless of how many tests execute.
38
+
39
+
40
+
## EnsureCreated vs Migrate
41
+
42
+
By default (when no `buildTemplate` is provided), `SqlInstance` uses `Database.EnsureCreatedAsync()` to create the schema. This is simpler but does not use migration history. If the project uses EF migrations, pass a `buildTemplate` that calls `Database.MigrateAsync()` instead. Do not mix both — `EnsureCreated` and `Migrate` are mutually exclusive in EF Core.
43
+
44
+
45
+
## Pending changes detection
46
+
47
+
When a `buildTemplate` delegate is provided via the context-based overload (`TemplateFromContext`), `SqlInstance` automatically calls `ThrowIfPendingChanges()` before building the template. This compares the current `DbContext` model against the latest migration snapshot and throws an `InvalidOperationException` listing the differences if any model changes have not been captured in a migration. This ensures the template database always matches the migration history and prevents silent schema drift during tests.
48
+
32
49
33
50
## Custom Migrations Operations
34
51
35
-
Optionally use [Custom Migrations Operations](https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/operations).
52
+
Optionally use [Custom Migrations Operations](https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/operations) by replacing `IMigrationsSqlGenerator` on the options builder before applying migrations.
<sup><ahref='/src/EfLocalDb.Tests/Snippets/Migrations.cs#L12-L16'title='Snippet source file'>snippet source</a> | <ahref='#snippet-IMigrationsSqlGenerator'title='Start of snippet'>anchor</a></sup>
43
60
<!-- endSnippet -->
44
61
62
+
This is useful for scenarios such as custom SQL generation, adding seed data via migration operations, or integrating with database-specific features not covered by the default SQL generator.
<sup><ahref='/src/EfLocalDb.Tests/Snippets/Migrations.cs#L18-L23'title='Snippet source file'>snippet source</a> | <ahref='#snippet-Migrate'title='Start of snippet'>anchor</a></sup>
57
76
<!-- endSnippet -->
77
+
78
+
This constructs a `DbContext` using the options builder (which is pre-configured to connect to the template database) and then applies all pending migrations. After this point the template database has the full schema and is ready to be cloned for individual tests.
[EntityFramework Migrations](https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/) are supported.
3
+
[EntityFramework Migrations](https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/) are supported via the `buildTemplate` parameter on `SqlInstance`. When using migrations, pass the `TemplateFromConnection` overload of `buildTemplate` so that the template database schema is created by applying migrations rather than `EnsureCreatedAsync`.
4
4
5
5
snippet: Migrations
6
6
7
7
The above performs the following actions:
8
8
9
+
* Creates a `SqlInstance` using the connection-based `buildTemplate` delegate.
10
+
* Optionally replaces `IMigrationsSqlGenerator` with a custom implementation.
11
+
* Constructs a `DbContext` from the provided `DbContextOptionsBuilder`.
12
+
* Applies all pending migrations via `Database.MigrateAsync()`.
13
+
14
+
The template database is built once and then cloned for each test, so migrations only run a single time regardless of how many tests execute.
15
+
16
+
17
+
## EnsureCreated vs Migrate
18
+
19
+
By default (when no `buildTemplate` is provided), `SqlInstance` uses `Database.EnsureCreatedAsync()` to create the schema. This is simpler but does not use migration history. If the project uses EF migrations, pass a `buildTemplate` that calls `Database.MigrateAsync()` instead. Do not mix both — `EnsureCreated` and `Migrate` are mutually exclusive in EF Core.
20
+
21
+
22
+
## Pending changes detection
23
+
24
+
When a `buildTemplate` delegate is provided via the context-based overload (`TemplateFromContext`), `SqlInstance` automatically calls `ThrowIfPendingChanges()` before building the template. This compares the current `DbContext` model against the latest migration snapshot and throws an `InvalidOperationException` listing the differences if any model changes have not been captured in a migration. This ensures the template database always matches the migration history and prevents silent schema drift during tests.
25
+
9
26
10
27
## Custom Migrations Operations
11
28
12
-
Optionally use [Custom Migrations Operations](https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/operations).
29
+
Optionally use [Custom Migrations Operations](https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/operations) by replacing `IMigrationsSqlGenerator` on the options builder before applying migrations.
13
30
14
31
snippet: IMigrationsSqlGenerator
15
32
33
+
This is useful for scenarios such as custom SQL generation, adding seed data via migration operations, or integrating with database-specific features not covered by the default SQL generator.
34
+
16
35
17
36
## Apply the migration
18
37
19
38
Perform a [Runtime apply of migrations](https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/#apply-migrations-at-runtime).
20
39
21
-
snippet: Migrate
40
+
snippet: Migrate
41
+
42
+
This constructs a `DbContext` using the options builder (which is pre-configured to connect to the template database) and then applies all pending migrations. After this point the template database has the full schema and is ready to be cloned for individual tests.
0 commit comments