Reputation: 83
I would like to be able to have a domain model with a different representation to the database model we are currently using.
At the moment we have a many to many relationship between a schedule and a line item, so a schedule has many line items and each line item can be reused across many schedules. In our code base though we only ever need to be concerned with a specific schedule, so the scenarios would be adding existing line items to a schedule or adding a new line item to a schedule, and alternately, getting a collection of line items for a given schedule.
In the database however we store the ordering of the line items on the mapping table. What I would like to do is represent this relationship in the doman model as a 1 to many relationship between schedule and line item and the line item domain model has an integer order property.
I can't seem to find any way to easily flatten this model using EF Code First and be able to put the property onto line item.
Essentially the database is:
Schedule
-Id
ScheduleLineItem
-ScheduleId
-LineItemId
-Order
LineItem
-Id
and the domain model I would like to use is:
Schedule
-Id
-List<LineItem>
LineItem
-Id
-Order
Upvotes: 1
Views: 204
Reputation: 364279
There is no way to flatten it in the mapping. You must map the model in the same way as database is designed and you can expose custom non mapped properties simulating non existing one-to-many relation.
For example:
public class Schedule
{
public int Id { get; set; }
public virtual ICollection<ScheduleLineItem> ScheduledItems { get; set; } // This is necessary for EF
public IEnumerable<LineItem> LineItems
{
return ScheduledItems.OrderBy(i => i.Order).Select(i => i.LineItem);
}
}
Upvotes: 1