2013-06-25 48 views
0

我使用實體框架5代碼第一,我有2個表:客戶和檔案。我有兩種關係類型多對多之間:實體框架代碼第一次雙倍多對多關係

public class Customer { 
    public int Id { get; set; } 
    public string Name { get; set; } 

    public List<Dossier> Debtors { get; set; } 
    public List<Dossier> Creditors { get; set; } 
} 

public class Dossier { 
    public int Id { get; set; } 
    public string Name { get; set; } 

    public List<Customer> Debtors { get; set; } 
    public List<Customer> Creditors { get; set; } 
} 

我怎麼能實現這個使用代碼第一?

感謝

+0

你將需要顯示你已經嘗試過 – devdigital

回答

2

我認爲你可以做到這一點使用兩個關係表,一個是債務人,一個是債權人。

modelBuilder.Entity<Customer>().HasMany(c => c.Debtors).WithMany(d => d.Debtors).Map(m => m.MapLeftKey("CustomerId").MapRightKey("DossierId").ToTable("DebtorCustomerDossierRel")); 

modelBuilder.Entity<Customer>().HasMany(c => c.Creditors).WithMany(d => d.Creditors).Map(m => m.MapLeftKey("CustomerId").MapRightKey("DossierId").ToTable("CreditorCustomerDossierRel")); 

我還沒有試過這段代碼,但它應該工作的非常接近。

這將在您的DbContext.OnModelCreating方法中(因爲這是Code First的新功能)。

+0

這就是我做到了這一點,它的工作原理。謝謝:) –