diff --git a/entity-framework/core/modeling/index.md b/entity-framework/core/modeling/index.md index 20f0e319cd..f50386baf3 100644 --- a/entity-framework/core/modeling/index.md +++ b/entity-framework/core/modeling/index.md @@ -33,6 +33,8 @@ Then just invoke the `Configure` method from `OnModelCreating`. [!code-csharp[Main](../../../samples/core/Modeling/Misc/EntityTypeConfiguration.cs?Name=ApplyIEntityTypeConfiguration)] +#### Applying all configurations in an assembly + It is possible to apply all configuration specified in types implementing `IEntityTypeConfiguration` in a given assembly. [!code-csharp[Main](../../../samples/core/Modeling/Misc/EntityTypeConfiguration.cs?Name=ApplyConfigurationsFromAssembly)] @@ -40,6 +42,44 @@ It is possible to apply all configuration specified in types implementing `IEnti > [!NOTE] > The order in which the configurations will be applied is undefined, therefore this method should only be used when the order doesn't matter. +#### Using `EntityTypeConfigurationAttribute` on entity types + +Rather than explicitly calling `Configure`, an can instead be placed on the entity type such that EF Core can find and use appropriate configuration. For example: + + +[!code-csharp[BookEntityType](../../../samples/core/Miscellaneous/NewInEFCore6/EntityTypeConfigurationAttributeSample.cs?name=BookEntityType)] + +This attribute means that EF Core will use the specified `IEntityTypeConfiguration` implementation whenever the `Book` entity type is included in a model. The entity type is included in a model using one of the normal mechanisms. For example, by creating a property for the entity type: + + +[!code-csharp[DbContext](../../../samples/core/Miscellaneous/NewInEFCore6/EntityTypeConfigurationAttributeSample.cs?name=DbContext)] + +Or by registering it in : + +```csharp +protected override void OnModelCreating(ModelBuilder modelBuilder) +{ + modelBuilder.Entity(); +} +``` + +> [!NOTE] +> `EntityTypeConfigurationAttribute` types will not be automatically discovered in an assembly. Entity types must be added to the model before the attribute will be discovered on that entity type. + ## Use data annotations to configure a model You can also apply certain attributes (known as _Data Annotations_) to your classes and properties. Data annotations will override conventions, but will be overridden by Fluent API configuration.