实体框架:是否可以从基本配置类继承?

Isk*_*yev 0 c# entity-framework

我正在通过 EF CodeFirst 为新数据库建模。这里我有一个抽象基类和从该类继承的每个域模型:

public abstract class Entity
{
    public Guid Id { get; set; }
    public byte [] TimeStamp { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

还有很多课程,例如:

public class Country : Entity
{
    public string Name { get; set; }
    public string InternationalCode { get; set; }
    public Location Location { get; set; }
    public ICollection<City> Cities { get; set; }

    public Country(string name, string internationalCode, decimal latitude, decimal longitude) 
        : this(name, internationalCode)
    {
        Location.Latitude = latitude;
        Location.Longitude = longitude;
    }
    public Country(string name, string internationalCode) : this()
    {
        Name = name;
        InternationalCode = internationalCode;
    }

    public Country()
    {
        Location = new Location();
        Cities = new List<City>();
    }
}
Run Code Online (Sandbox Code Playgroud)

以及基于 FluentAPI 的每个模型的配置:

public CountryConfiguration()
    {
        HasKey(p => p.Id);
        Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        Property(p => p.TimeStamp).IsRowVersion();

        Property(p => p.Name).IsRequired().HasMaxLength(100);
        Property(p => p.InternationalCode).IsRequired().HasMaxLength(5);

        HasMany(p => p.Cities)
            .WithOptional(p => p.BelongedCountry)
            .HasForeignKey(p => p.BelongedCountryId);
    }
Run Code Online (Sandbox Code Playgroud)

问题是

        HasKey(p => p.Id);
        Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        Property(p => p.TimeStamp).IsRowVersion();
Run Code Online (Sandbox Code Playgroud)

这篇文章对每个域模型复制粘贴了 30 多次。有没有办法让配置类继承EntityTypeConfiguration,同时也继承\包含实体配置的配置?

public class EntityConfiguration : EntityTypeConfiguration<Entity>
{
    public EntityConfiguration()
    {
        HasKey(p => p.Id);
        Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        Property(p => p.TimeStamp).IsRowVersion();
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道,C# 不允许多重继承。你有什么建议?

mxm*_*ile 5

Unless I'm missing something, can't you use an abstract class?

public abstract class BaseEntityConfiguration<T> : EntityTypeConfiguration<T> where T : Entity 
{
  protected BaseEntityConfiguration()
  {
      HasKey(p => p.Id);
      Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
      Property(p => p.TimeStamp).IsRowVersion();
  }
}

public class CountryConfiguration : BaseEntityConfiguration<Country>
{}
Run Code Online (Sandbox Code Playgroud)