如何在EF Code First中为所有常见实体配置使用基类?

时间:2013-04-10 11:10:51

标签: entity-framework ef-code-first entity-framework-5

我试图将我的实体配置(映射)类中常见的代码抽象为基类,如下所示:

public class EntityWithIdTypeConfig<T>: EntityTypeConfiguration<T> where T: class, IEntityWithId
{
    public EntityWithIdTypeConfig()
    {            
    }

    public EntityWithIdTypeConfig(string tableName, string schemaName)
    {
        ToTable(tableName, schemaName);
        HasKey(t => t.Id);
    }
}

示例实体类如下所示:

public partial class Parkade: IEntityWithId
{  
    public Parkade()
    {
        this.Zones = new List<Zone>();
    }
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Zone> Zones { get; set; }
}

这由类映射:

public class ParkadeConfig : EntityWithIdTypeConfig<Parkade> 
{
    public ParkadeConfig(string tableName, string schemaName):base(tableName, schemaName)
    {
        HasRequired(t => t.Zones)
            .WithMany();
        Property(t => t.Name)
            .IsRequired()
            .HasMaxLength(50);
    }
}

当我运行查询时,我收到错误:

The key component 'Id' is not a declared property on type 'Parkade'.

其中Id明确地在Parkade上声明。这可能是因为IdIEntityWithId的实现吗?我能做错什么?

0 个答案:

没有答案
相关问题