我试图将我的实体配置(映射)类中常见的代码抽象为基类,如下所示:
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上声明。这可能是因为Id
是IEntityWithId
的实现吗?我能做错什么?