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 ef3d87119a..91184ecdeb 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionInfoExtensions.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionInfoExtensions.cs @@ -37,5 +37,37 @@ namespace Volo.Abp.ObjectExtending } ); } + + public static ObjectExtensionInfo MapEfCoreProperty( + [NotNull] this ObjectExtensionInfo objectExtensionInfo, + [NotNull] string propertyName, + [CanBeNull] Action entityTypeAndPropertyBuildAction) + { + return objectExtensionInfo.MapEfCoreProperty( + typeof(TProperty), + propertyName, + entityTypeAndPropertyBuildAction + ); + } + + public static ObjectExtensionInfo MapEfCoreProperty( + [NotNull] this ObjectExtensionInfo objectExtensionInfo, + [NotNull] Type propertyType, + [NotNull] string propertyName, + [CanBeNull] Action entityTypeAndPropertyBuildAction) + { + Check.NotNull(objectExtensionInfo, nameof(objectExtensionInfo)); + + return objectExtensionInfo.AddOrUpdateProperty( + propertyType, + propertyName, + options => + { + options.MapEfCore( + entityTypeAndPropertyBuildAction + ); + } + ); + } } -} \ No newline at end of file +} 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 52dca0c105..06f360be6c 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionManagerExtensions.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionManagerExtensions.cs @@ -44,6 +44,42 @@ namespace Volo.Abp.ObjectExtending ); } + public static ObjectExtensionManager MapEfCoreProperty( + [NotNull] this ObjectExtensionManager objectExtensionManager, + [NotNull] string propertyName, + [CanBeNull] Action entityTypeAndPropertyBuildAction = null) + where TEntity : IHasExtraProperties, IEntity + { + return objectExtensionManager.MapEfCoreProperty( + typeof(TEntity), + typeof(TProperty), + propertyName, + entityTypeAndPropertyBuildAction + ); + } + + public static ObjectExtensionManager MapEfCoreProperty( + [NotNull] this ObjectExtensionManager objectExtensionManager, + [NotNull] Type entityType, + [NotNull] Type propertyType, + [NotNull] string propertyName, + [CanBeNull] Action entityTypeAndPropertyBuildAction = null) + { + Check.NotNull(objectExtensionManager, nameof(objectExtensionManager)); + + return objectExtensionManager.AddOrUpdateProperty( + entityType, + propertyType, + propertyName, + options => + { + options.MapEfCore( + entityTypeAndPropertyBuildAction + ); + } + ); + } + public static void ConfigureEfCoreEntity( [NotNull] this ObjectExtensionManager objectExtensionManager, [NotNull] EntityTypeBuilder typeBuilder) @@ -73,6 +109,7 @@ namespace Volo.Abp.ObjectExtending var propertyBuilder = typeBuilder.Property(property.Type, property.Name); + efCoreMapping.EntityTypeAndPropertyBuildAction?.Invoke(typeBuilder, propertyBuilder); efCoreMapping.PropertyBuildAction?.Invoke(propertyBuilder); } } diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionPropertyInfoExtensions.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionPropertyInfoExtensions.cs index fda5d88ceb..6e0c1ccc22 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionPropertyInfoExtensions.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionPropertyInfoExtensions.cs @@ -25,6 +25,22 @@ namespace Volo.Abp.ObjectExtending return propertyExtension; } + [NotNull] + public static ObjectExtensionPropertyInfo MapEfCore( + [NotNull] this ObjectExtensionPropertyInfo propertyExtension, + [CanBeNull] Action entityTypeAndPropertyBuildAction = null) + { + Check.NotNull(propertyExtension, nameof(propertyExtension)); + + propertyExtension.Configuration[EfCorePropertyConfigurationName] = + new ObjectExtensionPropertyInfoEfCoreMappingOptions( + propertyExtension, + entityTypeAndPropertyBuildAction: entityTypeAndPropertyBuildAction + ); + + return propertyExtension; + } + [CanBeNull] public static ObjectExtensionPropertyInfoEfCoreMappingOptions GetEfCoreMappingOrNull( [NotNull] this ObjectExtensionPropertyInfo propertyExtension) diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/ObjectExtensionPropertyInfoEfCoreMappingOptions.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/ObjectExtensionPropertyInfoEfCoreMappingOptions.cs index 4189c87962..15a818478b 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/ObjectExtensionPropertyInfoEfCoreMappingOptions.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/ObjectExtensionPropertyInfoEfCoreMappingOptions.cs @@ -15,13 +15,18 @@ namespace Volo.Abp.ObjectExtending [CanBeNull] public Action PropertyBuildAction { get; set; } + [CanBeNull] + public Action EntityTypeAndPropertyBuildAction { get; set; } + public ObjectExtensionPropertyInfoEfCoreMappingOptions( [NotNull] ObjectExtensionPropertyInfo extensionProperty, - [CanBeNull] Action propertyBuildAction = null) + [CanBeNull] Action propertyBuildAction = null, + [CanBeNull] Action entityTypeAndPropertyBuildAction = null) { ExtensionProperty = Check.NotNull(extensionProperty, nameof(extensionProperty)); - + PropertyBuildAction = propertyBuildAction; + EntityTypeAndPropertyBuildAction = entityTypeAndPropertyBuildAction; } } -} \ No newline at end of file +} diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/ExtraProperties_Tests.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/ExtraProperties_Tests.cs index 557116d158..338342f0cf 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/ExtraProperties_Tests.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/ExtraProperties_Tests.cs @@ -1,6 +1,10 @@ -using System.Threading.Tasks; +using System; +using System.Linq; +using System.Threading.Tasks; using Shouldly; using Volo.Abp.Data; +using Volo.Abp.Domain.Repositories; +using Volo.Abp.TestApp.Domain; using Volo.Abp.TestApp.Testing; using Xunit; @@ -28,5 +32,20 @@ namespace Volo.Abp.EntityFrameworkCore.Domain var london2 = await CityRepository.FindByNameAsync("London"); london2.GetProperty("PhoneCode").ShouldBe("53"); } + + + [Fact] + public async Task An_Extra_Property_Configured_As_Extension2() + { + await WithUnitOfWorkAsync(() => + { + var entityEntry = CityRepository.GetDbContext().Attach(new City(Guid.NewGuid(), "NewYork")); + var indexes = entityEntry.Metadata.GetIndexes().ToList(); + indexes.ShouldNotBeEmpty(); + indexes.ShouldContain(x => x.IsUnique); + return Task.CompletedTask; + }); + + } } } 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 3d415d79e6..fe81ee325b 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 @@ -15,8 +15,11 @@ namespace Volo.Abp.EntityFrameworkCore.Domain ObjectExtensionManager.Instance .MapEfCoreProperty( "PhoneCode", - p => p.HasMaxLength(8) - ); + (e, p) => + { + e.HasIndex(p.Metadata.Name).IsUnique(); + p.HasMaxLength(8); + }); }); } }