我有一个数据库集合模型,它从基类模型继承,如下所示。表结构不能正确地从基类中创建EF代码首先MVC
基类:
public abstract class BaseModel
{
[Key]
public int Id { get; set; }
public bool IsActive { get; set; }
public int ModifiedBy { get; set; }
public DateTime ModifiedDate { get; set; }
public int AddedBy { get; set; }
public DateTime AddedDate { get; set; }
}
子类:
public class AccountType : BaseModel
{
//[Key]
//public int Account_Type_Id { get; set; }
[Required]
[MaxLength(50)]
public string Account_Type_Name { get; set; }
[Required]
[MaxLength(10)]
public string Account_Type_Code { get; set; }
[MaxLength(100)]
public string AccountType_Description { get; set; }
}
而我的DbContext是如下:
public class BankApplicationContext :DbContext
{
public BankApplicationContext() : base("BankContextEntities")
{
Database.SetInitializer(new BankApplicationIntializer());
}
public virtual DbSet<AccountType> AccountTypes { get; set; }
public virtual DbSet<BaseModel> BaseModels { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<BaseModel>().ToTable("BaseModel");
modelBuilder.Entity<AccountType>().ToTable("AccountType");
}
}
在创建数据库中的表看起来像这样
但我需要从基类的子类,如下生成的所有审计列:
ACCOUNTTYPE:
标识
(的PK帐户类型)AccountTypeName
AccountTyp ECODE
AccountTypeDescription
IsActive
ModifiedBy
ModifiedDate
AddedBy
AddedDate
感谢您的答案Georg,得到这个错误“类型'AccountType'无法按照定义进行映射,因为它将继承的属性映射到使用实体分割或其他形式的继承的类型。可以选择不同的继承映射策略,以便不映射继承的属性,或更改层次结构中的所有类型以映射继承的属性,并且不使用分割。“ –
我明白了。我认为问题是由于我们试图映射抽象BaseModel而引起的。你需要访问抽象实体吗?否则你只能映射'AccountType'。请参阅https://stackoverflow.com/questions/13540976/multiple-inheritance-with-entity-framework-tpc –