我有一個數據庫集合模型,它從基類模型繼承,如下所示。表結構不能正確地從基類中創建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 –