diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Modeling/AbpEntityTypeBuilderExtensions.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Modeling/AbpEntityTypeBuilderExtensions.cs index 093d229848..2054d0e9db 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Modeling/AbpEntityTypeBuilderExtensions.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Modeling/AbpEntityTypeBuilderExtensions.cs @@ -16,7 +16,18 @@ namespace Volo.Abp.EntityFrameworkCore.Modeling { b.TryConfigureConcurrencyStamp(); b.TryConfigureExtraProperties(); - //TODO: Others + b.TryConfigureSoftDelete(); + b.TryConfigureDeletionTime(); + b.TryConfigureMayHaveCreator(); + b.TryConfigureMustHaveCreator(); + b.TryConfigureDeletionAudited(); + b.TryConfigureCreationTime(); + b.TryConfigureCreationAudited(); + b.TryConfigureLastModificationTime(); + b.TryConfigureModificationAudited(); + b.TryConfigureAudited(); + b.TryConfigureFullAudited(); + b.TryConfigureMultiTenant(); } public static void ConfigureConcurrencyStamp(this EntityTypeBuilder b) @@ -30,7 +41,7 @@ namespace Volo.Abp.EntityFrameworkCore.Modeling if (b.Metadata.ClrType.IsAssignableTo()) { //TODO: Max length? - b.Property(nameof(IHasConcurrencyStamp.ConcurrencyStamp)) + b.Property(nameof(IHasConcurrencyStamp.ConcurrencyStamp)) .IsConcurrencyToken() .HasColumnName(nameof(IHasConcurrencyStamp.ConcurrencyStamp)); } @@ -56,103 +67,197 @@ namespace Volo.Abp.EntityFrameworkCore.Modeling } } - //TODO: Others like ConfigureConcurrencyStamp/TryConfigureConcurrencyStamp and ConfigureExtraProperties/TryConfigureExtraProperties - public static void ConfigureSoftDelete(this EntityTypeBuilder b) where T : class, ISoftDelete { - b.Property(x => x.IsDeleted) - .IsRequired() - .HasDefaultValue(false) - .HasColumnName(nameof(ISoftDelete.IsDeleted)); + b.As().TryConfigureSoftDelete(); + } + + public static void TryConfigureSoftDelete(this EntityTypeBuilder b) + { + if (b.Metadata.ClrType.IsAssignableTo()) + { + b.Property(nameof(ISoftDelete.IsDeleted)) + .IsRequired() + .HasDefaultValue(false) + .HasColumnName(nameof(ISoftDelete.IsDeleted)); + } } public static void ConfigureDeletionTime(this EntityTypeBuilder b) where T : class, IHasDeletionTime { - b.ConfigureSoftDelete(); - b.Property(x => x.DeletionTime) - .IsRequired(false) - .HasColumnName(nameof(IHasDeletionTime.DeletionTime)); + b.As().TryConfigureDeletionTime(); + } + + public static void TryConfigureDeletionTime(this EntityTypeBuilder b) + { + if (b.Metadata.ClrType.IsAssignableTo()) + { + b.TryConfigureSoftDelete(); + b.Property(nameof(IHasDeletionTime.DeletionTime)) + .IsRequired(false) + .HasColumnName(nameof(IHasDeletionTime.DeletionTime)); + } } public static void ConfigureMayHaveCreator(this EntityTypeBuilder b) where T : class, IMayHaveCreator { - b.Property(x => x.CreatorId) - .IsRequired(false) - .HasColumnName(nameof(IMayHaveCreator.CreatorId)); + b.As().TryConfigureMayHaveCreator(); + } + + public static void TryConfigureMayHaveCreator(this EntityTypeBuilder b) + { + if (b.Metadata.ClrType.IsAssignableTo()) + { + b.Property(nameof(IMayHaveCreator.CreatorId)) + .IsRequired(false) + .HasColumnName(nameof(IMayHaveCreator.CreatorId)); + } } public static void ConfigureMustHaveCreator(this EntityTypeBuilder b) where T : class, IMustHaveCreator { - b.Property(x => x.CreatorId) - .IsRequired() - .HasColumnName(nameof(IMustHaveCreator.CreatorId)); + b.As().TryConfigureMustHaveCreator(); + } + + public static void TryConfigureMustHaveCreator(this EntityTypeBuilder b) + { + if (b.Metadata.ClrType.IsAssignableTo()) + { + b.Property(nameof(IMustHaveCreator.CreatorId)) + .IsRequired() + .HasColumnName(nameof(IMustHaveCreator.CreatorId)); + } } public static void ConfigureDeletionAudited(this EntityTypeBuilder b) where T : class, IDeletionAuditedObject { - b.ConfigureDeletionTime(); - b.Property(x => x.DeleterId) - .IsRequired(false) - .HasColumnName(nameof(IDeletionAuditedObject.DeleterId)); + b.As().TryConfigureDeletionAudited(); + } + + public static void TryConfigureDeletionAudited(this EntityTypeBuilder b) + { + if (b.Metadata.ClrType.IsAssignableTo()) + { + b.TryConfigureDeletionTime(); + b.Property(nameof(IDeletionAuditedObject.DeleterId)) + .IsRequired(false) + .HasColumnName(nameof(IDeletionAuditedObject.DeleterId)); + } } public static void ConfigureCreationTime(this EntityTypeBuilder b) where T : class, IHasCreationTime { - b.Property(x => x.CreationTime) - .IsRequired() - .HasColumnName(nameof(IHasCreationTime.CreationTime)); + b.As().TryConfigureCreationTime(); + } + + public static void TryConfigureCreationTime(this EntityTypeBuilder b) + { + if (b.Metadata.ClrType.IsAssignableTo()) + { + b.Property(nameof(IHasCreationTime.CreationTime)) + .IsRequired() + .HasColumnName(nameof(IHasCreationTime.CreationTime)); + } } public static void ConfigureCreationAudited(this EntityTypeBuilder b) where T : class, ICreationAuditedObject { - b.ConfigureCreationTime(); - b.ConfigureMayHaveCreator(); + b.As().TryConfigureCreationAudited(); + } + + public static void TryConfigureCreationAudited(this EntityTypeBuilder b) + { + if (b.Metadata.ClrType.IsAssignableTo()) + { + b.As().TryConfigureCreationTime(); + b.As().TryConfigureMayHaveCreator(); + } } public static void ConfigureLastModificationTime(this EntityTypeBuilder b) where T : class, IHasModificationTime { - b.Property(x => x.LastModificationTime) - .IsRequired(false) - .HasColumnName(nameof(IHasModificationTime.LastModificationTime)); + b.As().TryConfigureLastModificationTime(); + } + + public static void TryConfigureLastModificationTime(this EntityTypeBuilder b) + { + if (b.Metadata.ClrType.IsAssignableTo()) + { + b.Property(nameof(IHasModificationTime.LastModificationTime)) + .IsRequired(false) + .HasColumnName(nameof(IHasModificationTime.LastModificationTime)); + } } public static void ConfigureModificationAudited(this EntityTypeBuilder b) where T : class, IModificationAuditedObject { - b.ConfigureLastModificationTime(); - b.Property(x => x.LastModifierId) - .IsRequired(false) - .HasColumnName(nameof(IModificationAuditedObject.LastModifierId)); + b.As().TryConfigureModificationAudited(); + } + + public static void TryConfigureModificationAudited(this EntityTypeBuilder b) + { + if (b.Metadata.ClrType.IsAssignableTo()) + { + b.As().TryConfigureLastModificationTime(); + b.Property(nameof(IModificationAuditedObject.LastModifierId)) + .IsRequired(false) + .HasColumnName(nameof(IModificationAuditedObject.LastModifierId)); + } } public static void ConfigureAudited(this EntityTypeBuilder b) where T : class, IAuditedObject { - b.ConfigureCreationAudited(); - b.ConfigureModificationAudited(); + b.As().TryConfigureAudited(); + } + + public static void TryConfigureAudited(this EntityTypeBuilder b) + { + if (b.Metadata.ClrType.IsAssignableTo()) + { + b.As().TryConfigureCreationAudited(); + b.As().TryConfigureModificationAudited(); + } } public static void ConfigureFullAudited(this EntityTypeBuilder b) where T : class, IFullAuditedObject { - b.ConfigureAudited(); - b.ConfigureDeletionAudited(); + b.As().TryConfigureFullAudited(); + } + + public static void TryConfigureFullAudited(this EntityTypeBuilder b) + { + if (b.Metadata.ClrType.IsAssignableTo()) + { + b.As().TryConfigureAudited(); + b.As().TryConfigureDeletionAudited(); + } } public static void ConfigureMultiTenant(this EntityTypeBuilder b) where T : class, IMultiTenant { - b.Property(x => x.TenantId) - .IsRequired(false) - .HasColumnName(nameof(IMultiTenant.TenantId)); + b.As().TryConfigureMultiTenant(); + } + + public static void TryConfigureMultiTenant(this EntityTypeBuilder b) + { + if (b.Metadata.ClrType.IsAssignableTo()) + { + b.Property(nameof(IMultiTenant.TenantId)) + .IsRequired(false) + .HasColumnName(nameof(IMultiTenant.TenantId)); + } } //TODO: Add other interfaces (IAuditedObject...)