Skip to content

Commit 261cb46

Browse files
committed
docs
1 parent adf28d1 commit 261cb46

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

pages/efmigrations.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ To change this file edit the source file and then run MarkdownSnippets.
77

88
# EntityFramework Migrations
99

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`.
1111

1212
<!-- snippet: Migrations -->
1313
<a id='snippet-Migrations'></a>
@@ -29,10 +29,27 @@ var sqlInstance = new SqlInstance<MyDbContext>(
2929

3030
The above performs the following actions:
3131

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+
3249

3350
## Custom Migrations Operations
3451

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.
3653

3754
<!-- snippet: IMigrationsSqlGenerator -->
3855
<a id='snippet-IMigrationsSqlGenerator'></a>
@@ -42,6 +59,8 @@ options.ReplaceService<IMigrationsSqlGenerator, MigrationsGenerator>();
4259
<sup><a href='/src/EfLocalDb.Tests/Snippets/Migrations.cs#L12-L16' title='Snippet source file'>snippet source</a> | <a href='#snippet-IMigrationsSqlGenerator' title='Start of snippet'>anchor</a></sup>
4360
<!-- endSnippet -->
4461

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.
63+
4564

4665
## Apply the migration
4766

@@ -55,3 +74,5 @@ await data.Database.MigrateAsync();
5574
```
5675
<sup><a href='/src/EfLocalDb.Tests/Snippets/Migrations.cs#L18-L23' title='Snippet source file'>snippet source</a> | <a href='#snippet-Migrate' title='Start of snippet'>anchor</a></sup>
5776
<!-- 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.
Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,42 @@
11
# EntityFramework Migrations
22

3-
[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`.
44

55
snippet: Migrations
66

77
The above performs the following actions:
88

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+
926

1027
## Custom Migrations Operations
1128

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.
1330

1431
snippet: IMigrationsSqlGenerator
1532

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+
1635

1736
## Apply the migration
1837

1938
Perform a [Runtime apply of migrations](https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/#apply-migrations-at-runtime).
2039

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

Comments
 (0)