EF Code First - Relationships
Posted
by
CaffGeek
on Stack Overflow
See other posts from Stack Overflow
or by CaffGeek
Published on 2012-12-04T17:00:56Z
Indexed on
2012/12/04
17:03 UTC
Read the original article
Hit count: 212
.NET
|entity-framework-5
I have these classes
public class EntityBase : IEntity
{
public int Id { get; set; }
public DateTime Created { get; set; }
public string CreatedBy { get; set; }
public DateTime Updated { get; set; }
public string UpdatedBy { get; set; }
}
public class EftInterface : EntityBase
{
public string Name { get; set; }
public Provider Provider { get; set; }
public List<BusinessUnit> BusinessUnits { get; set; }
}
public class Provider : EntityBase, IEntity
{
public string Name { get; set; }
public decimal DefaultDebitLimit { get; set; }
public decimal DefaultCreditLimit { get; set; }
public decimal TreasuryDebitLimit { get; set; }
public decimal TreasuryCreditLimit { get; set; }
}
public class BusinessUnit : EntityBase
{
public string Name { get; set; }
}
An interface, is really a Provider, with a collection of Business Units. The issue is that while my db model ends up having a correct EftInterfaces table, with a FK to Provider_Id, the BusinessUnits table has a FK to EftInterface_Id.
But, a BusinessUnit can be included in more than one EftInterface.
I need a many to many relationship. A BusinessUnit can be part of many EftInterfaces, and an EftInterface can contain many BusinessUnits.
How can I get CodeFirst to generate the many-to-many table?
© Stack Overflow or respective owner