2014-12-02 71 views
0

在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」方法提供正確的關係數據嗎?

非常感謝提前! 蒙

+0

實際上你需要什麼? – Floremin 2014-12-02 19:20:51

回答

0

不知道這是否回答你的問題,但如果要指定在數據庫表之間的關係,使用EF代碼首先,你必須使用修改virtual爲你的「導航」屬性 - 那些映射到另一張桌子。所以代碼將如下所示:

[Table("Companys")] 
public class CompanyModel 
{ 
    // other properties and the rest of the code here 

    public virtual AccountCircleModel AccountCircle { get; set; } 
} 

[Table("AccountCircles")] 
public class AccountCircleModel 
{ 
    // other properties and the rest of the code here 

    public virtual CompanyModel Company { get; set; } 

    public virtual ICollection<AccountCirleMemberModel> Member { get; set; } 
} 

[Table("AccountCircleMember")] 
public class AccountCirleMemberModel 
{ 
    // other properties and the rest of the code here 

    public virtual AccountModel Account { get; set; } 
    public virtual AccountCircleModel AccountCircle { get; set; } 
} 

您不需要添加可用作外鍵的屬性 - EF將處理該問題。您可以指定它們,但是您必須使用流暢API將這些屬性映射爲特定表的外鍵。