Storing Entity Framework Entities in a Separate Assembly
- by Anthony Trudeau
The Entity Framework has been valuable to me since it came out, because it provided a convenient and powerful way to model against my data source in a consistent way. The first versions had some deficiencies that for me mostly fell in the category of the tight coupling between the model and its resulting object classes (entities).
Version 4 of the Entity Framework pretty much solves this with the support of T4 templates that allow you to implement your entities as self-tracking entities, plain old CLR objects (POCO), et al. Doing this involves either specifying a new code generation template or implementing them yourselves. Visual Studio 2010 ships with a self-tracking entities template and a POCO template is available from the Extension Manager. (Extension Manager is very nice but it's very easy to waste a bunch of time exploring add-ins. You've been warned.)
In a current project I wanted to use POCO; however, I didn't want my entities in the same assembly as the context classes. It would be nice if this was automatic, but since it isn't here are the simple steps to move them. These steps detail moving the entity classes and not the context. The context can be moved in the same way, but I don't see a compelling reason to physically separate the context from my model.
Turn off code generation for the template. To do this set the Custom Tool property for the entity template file to an empty string (the entity template file will be named something like MyModel.tt).
Expand the tree for the entity template file and delete all of its items. These are the items that were automatically generated when you added the template.
Create a project for your entities (if you haven't already).
Add an existing item and browse to your entity template file, but add it as a link (do not add it directly). Adding it as a link will allow the model and the template to stay in sync, but the code generation will occur in the new assembly.