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

扶醉桌前 提交于 2019-12-21 20:25:24

问题


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.


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/13955379/how-do-i-adapt-the-entity-framework-poco-t4-template-to-create-classes-in-a-sepa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!