From 3345e2e7bdebd897e5f63c85a2cc45afd8fd652f Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Tue, 11 May 2021 16:52:20 +0800 Subject: [PATCH] Support multiple usages --- .../EfCoreObjectExtensionInfoExtensions.cs | 50 +++++++++++-------- .../EfCoreObjectExtensionManagerExtensions.cs | 15 ++++-- .../Domain/TestEntityExtensionConfigurator.cs | 12 ++++- .../EntityFrameworkCore/TestAppDbContext.cs | 4 +- 4 files changed, 54 insertions(+), 27 deletions(-) diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionInfoExtensions.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionInfoExtensions.cs index 580e29b920..f27fc12d64 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionInfoExtensions.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionInfoExtensions.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; using JetBrains.Annotations; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; @@ -82,10 +84,19 @@ namespace Volo.Abp.ObjectExtending { Check.NotNull(objectExtensionInfo, nameof(objectExtensionInfo)); - objectExtensionInfo.Configuration[EfCoreEntityConfigurationName] = + var mappingOptionList = new List + { new ObjectExtensionInfoEfCoreMappingOptions( objectExtensionInfo, - entityTypeBuildAction); + entityTypeBuildAction) + }; + + objectExtensionInfo.Configuration.AddOrUpdate(EfCoreEntityConfigurationName, mappingOptionList, + (k, v) => + { + v.As>().Add(mappingOptionList.First()); + return v; + }); return objectExtensionInfo; } @@ -96,40 +107,39 @@ namespace Volo.Abp.ObjectExtending { Check.NotNull(objectExtensionInfo, nameof(objectExtensionInfo)); - objectExtensionInfo.Configuration[EfCoreDbContextConfigurationName] = + var mappingOptionList = new List + { new ObjectExtensionInfoEfCoreMappingOptions( objectExtensionInfo, - modelBuildAction); + modelBuildAction) + }; + + objectExtensionInfo.Configuration.AddOrUpdate(EfCoreDbContextConfigurationName, mappingOptionList, + (k, v) => + { + v.As>().Add(mappingOptionList.First()); + return v; + }); return objectExtensionInfo; } - [CanBeNull] - public static ObjectExtensionInfoEfCoreMappingOptions GetEfCoreEntityMappingOrNull( + public static List GetEfCoreEntityMappings( [NotNull] this ObjectExtensionInfo objectExtensionInfo) { Check.NotNull(objectExtensionInfo, nameof(objectExtensionInfo)); - if (!objectExtensionInfo.Configuration.TryGetValue(EfCoreEntityConfigurationName, out var options)) - { - return null; - } - - return options as ObjectExtensionInfoEfCoreMappingOptions; + return !objectExtensionInfo.Configuration.TryGetValue(EfCoreEntityConfigurationName, out var options) ? + new List() : options.As>(); } - [CanBeNull] - public static ObjectExtensionInfoEfCoreMappingOptions GetEfCoreDbContextMappingOrNull( + public static List GetEfCoreDbContextMappings( [NotNull] this ObjectExtensionInfo objectExtensionInfo) { Check.NotNull(objectExtensionInfo, nameof(objectExtensionInfo)); - if (!objectExtensionInfo.Configuration.TryGetValue(EfCoreDbContextConfigurationName, out var options)) - { - return null; - } - - return options as ObjectExtensionInfoEfCoreMappingOptions; + return !objectExtensionInfo.Configuration.TryGetValue(EfCoreDbContextConfigurationName, out var options) ? + new List() : options.As>(); } } } diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionManagerExtensions.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionManagerExtensions.cs index 0a22162104..a7e406f70f 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionManagerExtensions.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionManagerExtensions.cs @@ -163,8 +163,12 @@ namespace Volo.Abp.ObjectExtending return; } - var efCoreEntityMapping = objectExtension.GetEfCoreEntityMappingOrNull(); - efCoreEntityMapping?.EntityTypeBuildAction?.Invoke(typeBuilder); + var efCoreEntityMappings = objectExtension.GetEfCoreEntityMappings(); + + foreach (var efCoreEntityMapping in efCoreEntityMappings) + { + efCoreEntityMapping?.EntityTypeBuildAction?.Invoke(typeBuilder); + } foreach (var property in objectExtension.GetProperties()) { @@ -203,9 +207,12 @@ namespace Volo.Abp.ObjectExtending return; } - var efCoreDbContextMapping = objectExtension.GetEfCoreDbContextMappingOrNull(); + var efCoreDbContextMappings = objectExtension.GetEfCoreDbContextMappings(); - efCoreDbContextMapping?.ModelBuildAction?.Invoke(modelBuilder); + foreach (var efCoreDbContextMapping in efCoreDbContextMappings) + { + efCoreDbContextMapping?.ModelBuildAction?.Invoke(modelBuilder); + } } } } diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/TestEntityExtensionConfigurator.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/TestEntityExtensionConfigurator.cs index f2618966b8..a3b0004027 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/TestEntityExtensionConfigurator.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/TestEntityExtensionConfigurator.cs @@ -40,7 +40,12 @@ namespace Volo.Abp.EntityFrameworkCore.Domain ).MapEfCoreEntity(b => { b.As>() - .Property(x=>x.Name).IsRequired().HasMaxLength(200); + .Property(x=>x.Name).IsRequired(); + + }).MapEfCoreEntity(b => + { + b.As>() + .Property(x=>x.Name).HasMaxLength(200); }).MapEfCoreEntity(typeof(Person), b => { @@ -52,6 +57,11 @@ namespace Volo.Abp.EntityFrameworkCore.Domain { b.Entity().Property(x => x.Name).IsRequired(); }); + + ObjectExtensionManager.Instance.MapEfCoreDbContext(b => + { + b.Entity().Property(x => x.Name).IsRequired(); + }); }); } } diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs index 4d4f078839..2b4e5ec66c 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs @@ -32,8 +32,6 @@ namespace Volo.Abp.TestApp.EntityFrameworkCore base.OnModelCreating(modelBuilder); - modelBuilder.TryConfigureObjectExtensions(); - modelBuilder.Entity(b => { b.HasKey(p => new {p.PersonId, p.Number}); @@ -54,6 +52,8 @@ namespace Volo.Abp.TestApp.EntityFrameworkCore d.HasKey(x => new {x.CityId, x.Name}); }); }); + + modelBuilder.TryConfigureObjectExtensions(); } } }