在EF6 Code First(在MVC Web App中)遇到了一些問題。EF6 1:可選+1:很多
枚舉爲以 「AccountCircle」 賬戶分類:
public enum AccountType
{
Circle1,
Circle2,
Circle3,
Circle4,
Circle5
}
用於覈算主要類:
[Table("Accounts")]
public class AccountModel
{
public AccountModel()
{
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public string Name { get; set; }
public string EMail { get; set; }
}
主要公司-模型
[Table("Companys")]
public class CompanyModel
{
public CompanyModel()
{
this.AccountCircle = new AccountCircleModel();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public string Name { get; set; }
public int? idAccountCircle { get; set; }
public AccountCircleModel AccountCircle { get; set; }
}
類用於單個圈子:
[Table("AccountCircles")]
public class AccountCircleModel
{
public AccountCircleModel()
{
this.Member = new List<AccountCirleMemberModel>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public int idCompany { get; set; }
public CompanyModel Company { get; set; }
public List<AccountCirleMemberModel> Member { get; set; }
}
和最後但並非最不重要的帳戶本身具有additinal信息什麼類型的成員:
[Table("AccountCircleMember")]
public class AccountCirleMemberModel
{
public AccountCirleMemberModel()
{
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public AccountType Typ { get; set; }
public int idAccount { get; set; }
public virtual AccountModel Account { get; set; }
public int idAccountCircle { get; set; }
public AccountCircleModel AccountCircle { get; set; }
}
而且的DbContext
public class TestContext : DbContext
{
public TestContext()
: base()
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// modelBuilder Infos.....
base.OnModelCreating(modelBuilder);
}
#region Tables
public DbSet<AccountModel> Accounts { get; set; }
public DbSet<CompanyModel> Companys { get; set; }
public DbSet<AccountCircleModel> AccountCircles { get; set; }
#endregion
}
因此,有一個公司,它有一個可選屬性鍵入「AccountCircle」(1:可選) 在Accountcircle中,有一個帶有單獨枚舉的帳戶列表(AccountCirleMemberModel 1:many)
我試過了hundr modelBuilder方法的編輯給予EF6必要的信息,但沒有成功。 有人有提示,要給DbModelBuilder中的「protected override void OnModelCreating」方法提供正確的關係數據嗎?
非常感謝提前! 蒙
實際上你需要什麼? – Floremin 2014-12-02 19:20:51