How to adapt the Entity Framework POCO T4 template to create classes in a separate project?

I like to first save my code - or the generated T4 - POCOs in a project separate from DbContext. This helps me ensure that my entity classes are not associated with any data access service.

When I create a new EDMX model of a new database, the wizard creates a T4 template to create all POCOs in the same project as DbContext. How to change this template to add classes to a separate project?

Upon closer inspection, it would be much easier to move DbContext to a new project, but T4 doesn’t have a call for this fileManager.StartNewFile, so I don’t know where to start telling him to create the file elsewhere.

+2
source share
2 answers

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

This means that you don’t have to modify the template, as your model can see it.

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

The only drawback is that when you upgrade your model, you will have to manually run a custom tool.

+2
source

Id:

1.- Create a file in the 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 it to MyModel.tt ...

3.- WhateverModel.tt DAL proyect.

4.- MyModel.tt :

const string inputFile = @"WhateverModel.edmx";

5.- :

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

6.- ... .

.. 7. , , .

+1

Source: https://habr.com/ru/post/1502756/


All Articles