views:

559

answers:

3

I wonder how could I obtain an automatic mapping between entities generated by entity framework (.NET Framework 3.5 SP1) and custom domain model classes? I know I can create some data converters that translates the objects back and forth between the two worlds, but how could I accomplish this in an automatic way?

I started to develop my domain model classes, decorating them with validation attributes from Validation application block, which cannot be used inside the entity classes automatically generated (and refreshed) by the entity framework. Hence the need for automatically - or at least with minimal written code - mapping between EF and domain model.. any idea?

+1  A: 

With EF 4 this is now possible, You should have a look at Julie Lermann blog about EF . She recently made a whole series of post about POCO scenario and repositories with EF4 :

http://thedatafarm.com/blog/

With EF 3.5 this is hard, but you could still use Automapper if your class have the same set of attributes...

http://automapper.codeplex.com/

Stephane
I guess that EF 4 comes with .net 4 only?
lmsasu
yes, It does..So I guess you will have to stick to Automapper then... However, if you do a good job at separating the layers using Automapper, you could easily jump to EF4 when upgrading to .NET 4, since you will already have your POCO objects to plug into it, and probably some validation rules on top of it (using Dynamic Data maybe?)
Stephane
Does automapper perform bidirectionall mapping? that is, from domain model to entity and back?
lmsasu
You would not want to use AutoMapper to do entity->presentation because it doesn't do LINQ projections. It would work OK for bound edit model -> entity, though.
Craig Stuntz
+2  A: 

In EF 1.0, Automapper is a good choice for this: http://www.codeplex.com/AutoMapper

mkedobbs
+1  A: 

You can use Validation Application Block with generated entities, but you can't use the attribute based approach. Validation Application Block also allows you to use configuration files to define validations on entities (and contains a great Visual Studio plugin for editing this config file). I wrote about using VAB with Entitity Framework here. This article might be of some use to you.

Good luck.

Steven

related questions