Skip to content

Commit 93eb621

Browse files
CopilotAndriySvyryd
authored andcommitted
Document breaking changes for MigrationsNotFound default throw and EFOptimizeContext removal (#5282)
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
1 parent 2ac1627 commit 93eb621

2 files changed

Lines changed: 74 additions & 2 deletions

File tree

entity-framework/core/cli/msbuild.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: EF Core MSBuild tasks - EF Core
33
description: Reference guide for the Entity Framework Core .NET MSBuild tasks
44
author: AndriySvyryd
5-
ms.date: 01/17/2025
5+
ms.date: 03/04/2026
66
uid: core/cli/msbuild
77
---
88

@@ -30,14 +30,17 @@ If the project specifies `<PublishAot>true</PublishAot>` then by default the MSB
3030

3131
| MSBuild property | Description |
3232
|--------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
33-
| EFOptimizeContext | Set to `true` to enable MSBuild integration. |
33+
| EFOptimizeContext | Set to `true` to enable MSBuild integration. **EF Core 9-10 only; removed in EF Core 11.** |
3434
| EFScaffoldModelStage | Set to `publish`, `build` or `none` to indicate at which stage the compiled model will be generated. Defaults to `publish`. |
3535
| EFPrecompileQueriesStage | Set to `publish`, `build` or `none` to indicate at which stage the precompiled queries will be generated. Defaults to `publish`. |
3636
| DbContextName | The derived `DbContext` class to use. Class name only or fully qualified with namespaces. If this option is omitted, EF Core will perform generation for all context classes in the project. |
3737
| EFTargetNamespace | The namespace to use for all generated classes. If this option is omitted, EF Core will use `$(RootNamespace)`. |
3838
| EFOutputDir | The folder to put the generated files before the project is compiled. If this option is omitted, EF Core will use `$(IntermediateOutputPath)`. |
3939
| EFNullable | Whether nullable reference types will be used in the generated code. If this option is omitted, EF Core will use `$(Nullable)`. |
4040

41+
> [!NOTE]
42+
> Starting with EF Core 11, the `EFOptimizeContext` property has been [removed](xref:core/what-is-new/ef-core-11.0/breaking-changes#ef-optimize-context-removed). The `EFScaffoldModelStage` and `EFPrecompileQueriesStage` properties now work independently and don't require an additional enablement flag.
43+
4144
## Limitations
4245

4346
* When using the integration during the `publish` stage also set the rid in the startup project (e.g. \<RuntimeIdentifier\>win-x64\</RuntimeIdentifier\>)

entity-framework/core/what-is-new/ef-core-11.0/breaking-changes.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ This page documents API and behavior changes that have the potential to break ex
1919
| **Breaking change** | **Impact** |
2020
|:--------------------------------------------------------------------------------------------------------------- | -----------|
2121
| [Sync I/O via the Azure Cosmos DB provider has been fully removed](#cosmos-nosync) | Medium |
22+
| [EF Core now throws by default when no migrations are found](#migrations-not-found) | Low |
23+
| [`EFOptimizeContext` MSBuild property has been removed](#ef-optimize-context-removed) | Low |
2224
| [EF tools packages no longer reference Microsoft.EntityFrameworkCore.Design](#ef-tools-no-design-dep) | Low |
2325

2426
## Medium-impact changes
@@ -47,6 +49,73 @@ Convert your code to use async I/O APIs instead of sync I/O ones. For example, r
4749

4850
## Low-impact changes
4951

52+
<a name="migrations-not-found"></a>
53+
54+
### EF Core now throws by default when no migrations are found
55+
56+
[Tracking Issue #35218](https://github.com/dotnet/efcore/issues/35218)
57+
58+
#### Old behavior
59+
60+
Previously, when calling <xref:Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate*> or <xref:Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.MigrateAsync*> on a database with no migrations in the assembly, EF Core logged an informational message and returned without applying any changes.
61+
62+
#### New behavior
63+
64+
Starting with EF Core 11.0, EF Core throws an exception by default when no migrations are found in the assembly. This is consistent with the `PendingModelChangesWarning` behavior [introduced in EF 9.0](xref:core/what-is-new/ef-core-9.0/breaking-changes#pending-model-changes).
65+
66+
#### Why
67+
68+
Calling `Migrate()` or `MigrateAsync()` when no migrations exist typically indicates a misconfiguration. Rather than silently continuing and leaving the database in a potentially incorrect state, EF Core now alerts developers to this issue immediately.
69+
70+
#### Mitigations
71+
72+
If you intentionally call `Migrate()` without having any migrations (for example, because you manage the database schema through other means), remove the `Migrate()` call or suppress the exception by configuring warnings:
73+
74+
```csharp
75+
options.ConfigureWarnings(w => w.Ignore(RelationalEventId.MigrationsNotFound))
76+
```
77+
78+
Or to log the event instead of throwing:
79+
80+
```csharp
81+
options.ConfigureWarnings(w => w.Log(RelationalEventId.MigrationsNotFound))
82+
```
83+
84+
<a name="ef-optimize-context-removed"></a>
85+
86+
### `EFOptimizeContext` MSBuild property has been removed
87+
88+
[Tracking Issue #35079](https://github.com/dotnet/efcore/issues/35079)
89+
90+
#### Old behavior
91+
92+
Previously, the `EFOptimizeContext` MSBuild property could be set to `true` to enable compiled model and precompiled query code generation during build or publish:
93+
94+
```xml
95+
<EFOptimizeContext Condition="'$(Configuration)'=='Release'">true</EFOptimizeContext>
96+
```
97+
98+
#### New behavior
99+
100+
Starting with EF Core 11.0, the `EFOptimizeContext` MSBuild property has been removed. Code generation is now controlled exclusively through the `EFScaffoldModelStage` and `EFPrecompileQueriesStage` properties. When `PublishAOT` is set to `true`, code generation is automatically enabled during publish without needing any additional property.
101+
102+
#### Why
103+
104+
The `EFScaffoldModelStage` and `EFPrecompileQueriesStage` properties already provide fine-grained control over when code generation occurs. `EFOptimizeContext` was a redundant enablement gate.
105+
106+
#### Mitigations
107+
108+
Replace usages of `EFOptimizeContext` with the `EFScaffoldModelStage` and `EFPrecompileQueriesStage` properties. These can be set to `publish` or `build` to control at which stage code generation occurs:
109+
110+
```xml
111+
<EFScaffoldModelStage>publish</EFScaffoldModelStage>
112+
<EFPrecompileQueriesStage>publish</EFPrecompileQueriesStage>
113+
```
114+
115+
Any other value (for example, `none`) disables the corresponding generation.
116+
117+
If you have `PublishAOT` set to `true`, code generation is automatically enabled during publish and no additional configuration is needed.
118+
50119
<a name="ef-tools-no-design-dep"></a>
51120

52121
### EF tools packages no longer reference Microsoft.EntityFrameworkCore.Design

0 commit comments

Comments
 (0)