I\'m trying to use the Repository Pattern with EF4 using VS2010.
To this end I am using POCO code generation by right clicking on the entity model designer and click
Actually the T4 templates in EF 4.0 were designed with this scenario in mind :)
There are 2 templates:
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
@Nick,
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
For EF5 + DbContext generator: It's easy to move your Name.Context.tt to a different project. However, you will need to reference the model classes. You can do this manually, but this will require you to change it every time code gets generated. You could also use the same namespace for both projects. This is valid and will work, but I think this is bad design. Another alternative is to change the T4 template (Name.Context.tt).
Change this (line 43):
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
<#
if (container.FunctionImports.Any())
{
#>
To this:
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
<#
if (modelNamespace != codeNamespace)
#>
using <#=code.EscapeNamespace(modelNamespace)#>;
<#
if (container.FunctionImports.Any())
{
#>
This will check if your model namespace differs from your code namespace, if so, it will insert the required using to reference your model classes.