1

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

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'.

在 Parkade 上清楚地声明了 ID。这可能是因为IdIEntityWithId? 我可能做错了什么?

4

0 回答 0