Generate POCO classes in different project to the project with Entity Framework model

Solution 1:

Actually the T4 templates in EF 4.0 were designed with this scenario in mind :)

There are 2 templates:

  • One for the Entities themselves (i.e. ModelName.tt)
  • One for the ObjectContext (i.e. ModelName.Context.tt)

You should put the ModelName.tt file in you POCO project, and just change the template to point to the EDMX file in the persistence aware project.

Sounds weird I know: There is now a dependency, but it is at T4 generation time, not at compile time! And that should be okay? Because the resulting POCO assembly is still completely persistence ignorant.

See steps 5 & 6 of this: http://blogs.msdn.com/adonet/pages/walkthrough-poco-template-for-the-entity-framework.aspx for more.

Hope this helps

Alex

Solution 2:

@Nick,

  1. To force the regeneration of the POCO entities, you just need to right-click the main .tt file and select "Run Custom Tool". This will force it to regenerate your POCO classes with your updated changes to the .edmx model.
  2. Is there any problem with you going ahead and Right-clicking the model and selecting "Generate Database from Model..." even though you aren't necessarily generating the database? That will most likely get rid of your 'Error 11007...'.
  3. I think it's equivalent to a "Code Behind". I don't know any more than that.

One other thing to note about the link that Alex gave. Once I moved my main .tt file to a different project, the file that was generated from the ".Context.tt" file would not compile because it was missing references to the POCO files that were located in a different namespace (because I wanted my ObjectContext to be in a different domain than my POCO files). I had to modify the ".Context.tt" file to had a using Poco.Namespace (where Poco.Namespace is the name of the namespace where the POCO files were generated). This then allowed my project to compile.

Joel