I have this aggregate root
CustomerGroup
int Id
string Name
Customer[] Customers
And this one
Customer
int Id
string Name
My model was built using EF 4. What i’d prefer would be (for sticking to the concept of aggregate root)
CustomerGroup
int Id
string Name
int[] Customers
How would you do this ?
A customer might be in many gorup (many to many relationship) but I only need the relationship in one way group -> customer. I don’t have any use case where I need customer -> group.
As you have a many-to-many association between
CustomerandCustomerGroupthere also is a so called junction table, let’s sayCustomerGroupCustomer, that hasCustomerIdandCustomerGroupId.If this table only has these two fields EF can use it implicitly, which means that
CustomerGroupcan have aCustomerscollection where in the mapping you indicate thatCustomerGroupCustomeris the junction table. Here is an example. You can, but don’t have to, mapCustomer.CustomerGroups. When working database-first, this implicit behaviour is the default.However, since you’re interested in
CustomerIds only you could decide to have a regular one-to-many associationCustomerGroup.CustomerGroupCustomers. If you address this navigation property only one join is required and the query is pretty light. If you have aCustomerGroupinstance you can doand only the
CustomerIds will be queried.If you want you can get the
Customersdirectly by doing