Use IEntityTypeConfiguration with a base entity

后端 未结 4 995
醉梦人生
醉梦人生 2021-02-05 14:40

In EF Core 2.0, we have the ability to derive from IEntityTypeConfiguration for cleaner Fluent API mappings (source).

How can I extend this pattern to utili

4条回答
  •  情深已故
    2021-02-05 14:40

    There is another way to solve the problem, and that is to use Template Method Design Pattern. Like this:

    public abstract class BaseEntityTypeConfiguration : IEntityTypeConfiguration
        where TBase : BaseEntity
    {
        public void Configure(EntityTypeBuilder entityTypeBuilder)
        {
            //Base Configuration
    
            ConfigureOtherProperties(builder);
        }
    
        public abstract void ConfigureOtherProperties(EntityTypeBuilder builder);
    }
    
    public class MaintainerConfiguration : BaseEntityTypeConfiguration
    {
        public override void ConfigureOtherProperties(EntityTypeBuilder entityTypeBuilder)
        {
            entityTypeBuilder.Property(b => b.CreatedDateUtc).HasDefaultValueSql("CURRENT_TIMESTAMP");        
        }
    }
    

    With this way you don't need to write any single line in child configuration.

提交回复
热议问题