From 78250886a3be74710aa82d6625dc065dc9738e09 Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 4 Jun 2021 15:52:37 +0800 Subject: [PATCH 1/2] Add document of customizing EF Core mapping of Entity&DbContext. --- docs/en/Entity-Framework-Core.md | 70 ++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 8 deletions(-) diff --git a/docs/en/Entity-Framework-Core.md b/docs/en/Entity-Framework-Core.md index fcbf5d8091..7fe42e76ce 100644 --- a/docs/en/Entity-Framework-Core.md +++ b/docs/en/Entity-Framework-Core.md @@ -604,23 +604,77 @@ ObjectExtensionManager.Instance ); ```` -If the related module has implemented this feature (by using the `ConfigureEfCoreEntity` explained below), then the new property is added to the model. Then you need to run the standard `Add-Migration` and `Update-Database` commands to update your database to add the new field. +### MapEfCoreEntity ->`MapEfCoreProperty` method must be called before using the related `DbContext`. It is a static method. The best way is to use it in your application as earlier as possible. The application startup template has a `YourProjectNameEfCoreEntityExtensionMappings` class that is safe to use this method inside. +`MapEfCoreEntity` is a shortcut extension method to configure the `Entity`. -### ConfigureEfCoreEntity +**Example**: Set the max length of `Name` to the `IdentityRole` entity: -If you are building a reusable module and want to allow application developers to add properties to your entities, you can use the `ConfigureEfCoreEntity` extension method in your entity mapping. However, there is a shortcut extension method `ConfigureObjectExtensions` that can be used while configuring the entity mapping: +````csharp +ObjectExtensionManager.Instance + .MapEfCoreEntity(builder => + { + builder.As>().Property(x => x.Name).HasMaxLength(200); + }); +```` + +### MapEfCoreDbContext + +`MapEfCoreDbContext` is a shortcut extension method to configure the `DbContext`. + +**Example**: Set the max length of `Name` to the `IdentityRole` entity of `IdentityDbContext`: ````csharp -builder.Entity(b => +ObjectExtensionManager.Instance.MapEfCoreDbContext(b => { - b.ConfigureObjectExtensions(); - //... + b.Entity().Property(x => x.Name).HasMaxLength(200); }); ```` -> If you call `ConfigureByConvention()` extension method (like `b.ConfigureByConvention()` for this example), ABP Framework internally calls the `ConfigureObjectExtensions` method. It is a **best practice** to use the `ConfigureByConvention()` method since it also configures database mapping for base properties by convention. +If the related module has implemented this feature(explained below), then the new property is added to the model or the DbContext/Entity configure changed. Then you need to run the standard `Add-Migration` and `Update-Database` commands to update your database to add the new field. + +> The `MapEfCoreProperty`, `MapEfCoreEntity` and `MapEfCoreDbContext` methods must be called before using the related `DbContext`. It is a static method. The best way is to use it in your application as earlier as possible. The application startup template has a `YourProjectNameEfCoreEntityExtensionMappings` class that is safe to use this method inside. + +### ConfigureEfCoreEntity, ApplyObjectExtensionMappings and TryConfigureObjectExtensions + +If you are building a reusable module and want to allow application developers to add properties to your entities, you can use the `ConfigureEfCoreEntity`, `ApplyObjectExtensionMappings` and `TryConfigureObjectExtensions` extension methods in your entity mapping. + +**Example**: +````csharp +public static class QADbContextModelCreatingExtensions +{ + public static void ConfigureQA( + this ModelBuilder builder, + Action optionsAction = null) + { + Check.NotNull(builder, nameof(builder)); + + var options = new QAModelBuilderConfigurationOptions( + QADatabaseDbProperties.DbTablePrefix, + QADatabaseDbProperties.DbSchema + ); + + optionsAction?.Invoke(options); + + builder.Entity(b => + { + b.ToTable(options.TablePrefix + "Questions", options.Schema); + b.ConfigureByConvention(); + //... + + //Call this in the end of buildAction. + b.ApplyObjectExtensionMappings(); + }); + + //... + + //Call this in the end of ConfigureQA. + builder.TryConfigureObjectExtensions(); + } +} +```` + +> If you call `ConfigureByConvention()` extension method (like `b.ConfigureByConvention()` for this example), ABP Framework internally calls the `ConfigureObjectExtensions` and `ConfigureEfCoreEntity` methods. It is a **best practice** to use the `ConfigureByConvention()` method since it also configures database mapping for base properties by convention. See the "*ConfigureByConvention Method*" section above for more information. From 9cd770e4c326d1fd62654513dff4d8f0beb5b1e9 Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 4 Jun 2021 15:57:08 +0800 Subject: [PATCH 2/2] Update Entity-Framework-Core.md --- docs/en/Entity-Framework-Core.md | 44 ++++++++++++++++---------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/docs/en/Entity-Framework-Core.md b/docs/en/Entity-Framework-Core.md index 7fe42e76ce..f60fcfc490 100644 --- a/docs/en/Entity-Framework-Core.md +++ b/docs/en/Entity-Framework-Core.md @@ -643,34 +643,34 @@ If you are building a reusable module and want to allow application developers t ````csharp public static class QADbContextModelCreatingExtensions { - public static void ConfigureQA( - this ModelBuilder builder, - Action optionsAction = null) - { - Check.NotNull(builder, nameof(builder)); - - var options = new QAModelBuilderConfigurationOptions( - QADatabaseDbProperties.DbTablePrefix, - QADatabaseDbProperties.DbSchema - ); - - optionsAction?.Invoke(options); - - builder.Entity(b => - { - b.ToTable(options.TablePrefix + "Questions", options.Schema); - b.ConfigureByConvention(); + public static void ConfigureQA( + this ModelBuilder builder, + Action optionsAction = null) + { + Check.NotNull(builder, nameof(builder)); + + var options = new QAModelBuilderConfigurationOptions( + QADatabaseDbProperties.DbTablePrefix, + QADatabaseDbProperties.DbSchema + ); + + optionsAction?.Invoke(options); + + builder.Entity(b => + { + b.ToTable(options.TablePrefix + "Questions", options.Schema); + b.ConfigureByConvention(); //... //Call this in the end of buildAction. - b.ApplyObjectExtensionMappings(); - }); + b.ApplyObjectExtensionMappings(); + }); //... //Call this in the end of ConfigureQA. - builder.TryConfigureObjectExtensions(); - } + builder.TryConfigureObjectExtensions(); + } } ```` @@ -866,4 +866,4 @@ public class MyCustomEfCoreBulkOperationProvider ## See Also * [Entities](Entities.md) -* [Repositories](Repositories.md) \ No newline at end of file +* [Repositories](Repositories.md)