EF Code Code 4.2中每个具体类型映射问题的表格

时间:2012-07-12 04:10:04

标签: entity-framework ef-code-first

我收到EntityType 'User' has no key defined. Define the key for this EntityType错误。

型号:

public abstract class KeyedEntityBase
{
    public int ID { get; private set; }
}

public class User : KeyedEntityBase
{
    public string UserName { get; private set; }
    public string EmailAddress { get; private set; }
}

上下文:

public class LSBPortalContext : DbContext
{
    public LSBPortalContext()
        : base("LSBPortalDB")
    {
    }

    public DbSet<User> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>().Map(m =>
        {
            m.MapInheritedProperties();
            m.ToTable("User");
        });

        base.OnModelCreating(modelBuilder);
    }
}

1 个答案:

答案 0 :(得分:1)

问题是我实际上没有映射KeyedEntityBase类。一旦我这样做,一切都正常。