Reputation: 87
I'm using Entity framwork 5 code first and i have 2 tables: Customer and Dossier. And i have 2 relationships of type many-to-many between them:
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; }
}
How can i achieve this using code first?
Thanks
Upvotes: 0
Views: 439
Reputation: 856
I think you can achieve that using two relation tables, one for the Debtors and one for the Creditors.
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"));
I haven't tried this code, but it should work pretty close to that.
This would be in your DbContext.OnModelCreating method (for that are new to Code First).
Upvotes: 2