views:

32

answers:

1

Hello,

I am looking to setup architecture for entity framework that will break apart the entities into multiple models. What I am wondering if it is possible to code-generate one set of entities, so that each model uses the same set of data access classes? I realize the issue with contexts, but I was wondering if each context really needed to have its own set of classes, or I can create many contexts but only have one set of classes that represent the backend tables, using the self-tracking entities generation feature.

Thanks.

A: 

You can definitely share POCO classes across multiple models.

For example a class like this:

public class Person
{
   public int ID {get;set;}
   public string Firstname {get;set;}
   public string Surname {get;set;}
   public string Lastname {get {return Surname;} set {Surname = value;}}
}

Would work in an EDMX that defines Person as ID,Firstname,Surname And would work in a second EDMX that defines Person as ID,Firstname,Lastname

Not sure though about Self-Tracking Entities, STEs are 'POCO' but they also have some model specific code which might break if the definition of the EntityType is different in your two EDMXs (like in my above sample).

You'd have to try it out.

Hope this helps

Alex (Former EF team member)

Alex James
"might break if the definition of the EntityType is different in your two EDMX" - do you mean is the entity structure is different? These will all be mapping the same database, so the signature would be the same.... THX
Brian
When you have two models, if you have an entity that straddles both (and is thus defined in both) the structure of the entity from the perspective of each model maybe different, because in each model the entity only links to entities in that model. The other properties used in the other model are essentially dangling from the perspective of the current model.
Alex James