3

I like to to keep my code first - or T4 generated - POCOs in a project separated from the DbContext. This helps me ensure my entity classes aren't coupled to any one data access service.

When I create a new DB first EDMX model, the wizard generates a T4 template to generate all the POCOs in the same project as the DbContext. How can I modify this template to add classes to a separate project?

On closer inspection, it would probably be much easier to move the DbContext to a new project, but the T4 for this has no call to fileManager.StartNewFile so i don't know where to begin telling it to create a file elsewhere.

ProfK
  • 49,207
  • 121
  • 399
  • 775
  • ProfK, can you clarify how you were able to get poco to be generated into a separate project? I'm trying to do the same using EF and onion – jr3 Oct 16 '13 at 18:33
  • @jr3 I don't really recall, but I think I just changed the output path of the .tt that the EF wizard uses. – ProfK Oct 17 '13 at 06:19

2 Answers2

3

You can exclude the .tt file from your DAL project and then add it as a link in another project.

This means you won't have to alter the template as it can see your model.

The files produced when you run the template will be included in your data objects assembly, although the physical files will be in your DAL project.

The only downside is that you will have to manually run the custom tool when you update your model.

Nick Butler
  • 24,045
  • 4
  • 49
  • 70
  • 1
    @atconway right-click the `.tt` file in solution explorer and "Run Custom Tool" – Nick Butler Jan 04 '13 at 16:45
  • Since the physical files reside in a separate project now (as desired) but my `MyModel.context.cs` file is in the original project with the .edmx, how do I let it know where the files have been relocated to? I added a reference to the separate project, but without placing a `using` statement in my Context file to the new project namespace, lines like this fail: `public DbSet People { get; set; }` it needs to look like `public DbSet People { get; set; }` This is auto generated code though and I don't want to do this. – atconway Jan 04 '13 at 20:02
  • Just figured this out - The **Custom Tool Namespace** value on the Context.tt properties can be updated to the proper reference, i.e. `MyProject.DAL`, and the references will resolve. – atconway Jan 04 '13 at 20:16
2

Id say:

1.- Create a file in your model project (MyProject.Model proyect), a .tt file with your desired name... (MyModel.tt for this example)

2.- Go to DAL proyect, open the WhateverModel.tt file and copy the content to MyModel.tt...

3.- Delete WhateverModel.tt form DAL proyect.

4.- Look in MyModel.tt for:

const string inputFile = @"WhateverModel.edmx";

5.- And replace it with:

const string inputFile = @"..\TheRelativeRouteToYourEdmxFileGoesHere.edmx";

6.- And... Run custom tool to generate your model.

Note: 7.- For sure you will have to change few using directives and namespaces but it works for me.

Juan
  • 2,156
  • 18
  • 26