Map DDD classes to database model generated from Entity Framework 4.0
I would like to find out if anyone can give me a hand on this problem that i am currently having.
Basically, I have a legacy db that I have to deal with. I have created some domain classes to represent our db model. However, the domain classes are not a straight one to one mapping with the database tables.
This is a snapshot of legacy db model:
and one of my domain classes is like this:
public class Reading
{开发者_JAVA百科
public Location Location {get; set;}
}
public class Location
{
public string Name { get; set;}
}
Given the above model and domain class, how can I map them?
Most of the articles that I found out there have straight one to one mapping.
If any of you can give some help on this, that would be much appreciated.
Thanks in advance.
EF supports "table per type" (one to one mapping), "table per hierarchy", "table per concrete type", "table splitting" (into more than one entity) and "entity splitting" (into more than one table).
Your reading tables seem to suit table per concrete type.
Each of the mapping types can be found easily in Google. - Search for "entity framework" "table per concrete type".
You can use AutoMapper and write a Custom Value Resolver to map and aggregate the objects.
See http://automapper.codeplex.com, and more specifically, http://automapper.codeplex.com/wikipage?title=Custom%20Value%20Resolvers for the Custom Action Resolvers.
You cannot map them directly by Entity Framework because your classes in domain model are not entities (ok you probably can but that can have vary bad consequences if location's Name
is not unique). First problem is that your entities must have primary key mapped and primary key must be unique (I'm absolutely not sure what should Reading
entity represent).
Most articles describe mapping of whole tables because that is the easiest way to use EF. Actually mapping only partial table data in EDMX means writing EDMX XML manually which is pain in the a**. You can solve this by using EF 4.1 and fluent api (no EDMX). If you want to map only partial data and still be able to insert new records non-mapped columns it tables must be nullable.
精彩评论