Skip to content

Commit c05e1bd

Browse files
Copilotroji
authored andcommitted
Document ExcludeForeignKeyFromMigrations (#5283)
Document dotnet/efcore#15854
1 parent 93eb621 commit c05e1bd

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

entity-framework/core/modeling/relationships/foreign-and-principal-keys.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,32 @@ This can be changed in the model building API using `HasConstraintName`. For exa
189189
> [!TIP]
190190
> The constraint name is not used by the EF runtime. It is only used when creating a database schema using [EF Core Migrations](xref:core/managing-schemas/migrations/index).
191191
192+
### Excluding foreign key constraints from migrations
193+
194+
> [!NOTE]
195+
> This feature is being introduced in EF Core 11, which is currently in preview.
196+
197+
Sometimes it is useful to have the foreign key relationship represented in the EF model, but without creating the corresponding foreign key constraint in the database. This can happen with legacy databases where constraints don't exist, or in data synchronization scenarios where the order of inserting related entities might temporarily violate referential integrity constraints. In these cases, use `ExcludeForeignKeyFromMigrations` to prevent EF from generating the foreign key constraint in migrations (and `EnsureCreated`):
198+
199+
```csharp
200+
modelBuilder.Entity<Blog>()
201+
.HasMany(e => e.Posts)
202+
.WithOne(e => e.Blog)
203+
.HasForeignKey(e => e.BlogId)
204+
.ExcludeForeignKeyFromMigrations();
205+
```
206+
207+
With this configuration, EF will not create a foreign key constraint in the database, but the relationship is still tracked in the EF model and can be used normally for loading related data, change tracking, etc. EF will still create a database index for the foreign key column, since indexes benefit queries regardless of whether a constraint exists.
208+
209+
To apply this across all foreign keys in the model (e.g. to globally disable all foreign key constraints), you can iterate over all foreign keys in `OnModelCreating`:
210+
211+
```csharp
212+
foreach (var foreignKey in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
213+
{
214+
foreignKey.SetIsExcludedFromMigrations(true);
215+
}
216+
```
217+
192218
### Indexes for foreign keys
193219

194220
By convention, EF creates a database index for the property or properties of a foreign key. See [_Model building conventions_](xref:core/modeling/relationships/conventions) for more information about the types of indexes created by convention.

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,24 @@ This feature was contributed by [@JoasE](https://github.com/JoasE) - many thanks
197197

198198
## Migrations
199199

200+
<a name="migrations-exclude-fk"></a>
201+
202+
### Excluding foreign key constraints from migrations
203+
204+
It is now possible to configure a foreign key relationship in the EF model while preventing the corresponding database constraint from being created by migrations. This is useful for legacy databases without existing constraints, or in data synchronization scenarios where referential integrity constraints might conflict with the synchronization order:
205+
206+
```csharp
207+
modelBuilder.Entity<Blog>()
208+
.HasMany(e => e.Posts)
209+
.WithOne(e => e.Blog)
210+
.HasForeignKey(e => e.BlogId)
211+
.ExcludeForeignKeyFromMigrations();
212+
```
213+
214+
The relationship is fully supported in EF for queries, change tracking, etc. Only the foreign key constraint in the database is suppressed; a database index is still created on the foreign key column.
215+
216+
For more information, see [Excluding foreign key constraints from migrations](xref:core/modeling/relationships/foreign-and-principal-keys#excluding-foreign-key-constraints-from-migrations).
217+
200218
<a name="migrations-snapshot-latest-id"></a>
201219

202220
### Latest migration ID recorded in model snapshot

0 commit comments

Comments
 (0)