문제

I've seen several answers on this question on how to one can move the model.tt (which contains the POCO entity classes ) file to a separate project in Vs2010

This doesn't seem to work in EF5 and it seems like the reason is that the model.tt file is a subitem of the edmx file.

How can I achieve the same in Entity Framework 5 / VS 2012?

도움이 되었습니까?

해결책

Best answer that I know of is to simply cut the model.tt (insert appropriate name) file from the current project and add it to the desired project. I have gone so far as to rewrite the .tt files that EF uses for exactly that reason.

I am a fan of separation of concerns and put appropriate .tt files in the following projects: DataAccess (model.context.tt), Entity (model.tt) and Repository (model.repository.tt). Of course, you have to fiddle with the content of the .tt file to point it to the .edmx but that's trivial. To cause all the t4 transforms to take place, there is an option in VS2012, Build | Transform all T4, that will process all of the T4 files in a solution.

Follow this process:

Create your .edmx if it doesn't already exist.

Right click, select "Add Code Generation Item" to add the EF DbContext 5.0 generator if you haven't already done so (I'm presuming that's the one you want to tinker with but this process works with any t4).

This will get you your two .tt files - the model and the model.context.

Drop to a windows explorer, find the model.tt file in the directory, copy it to your "Entity" directory (wherever you want your t4 to generate to).

Back in VS, delete it from the .edmx project and "Add Existing Item" to your new project.

If, for instance, your two projects are named as above (DataAccess to contain the .edmx, Entity to contain the model information), then you need to change the name of the input file in the model.tt file in the value (line 5) "const string inputFile = "name of .edmx" to "..\Directory containing .edmx\name of .edmx".

Run the T4 and it just works.

다른 팁

If you mean that the code is not generated it is a bug. This is the bug we use for tracking it http://entityframework.codeplex.com/workitem/453 - it contains some workarounds. Another option is enforce code generation by right-clicking the tt file and selecting "Run Custom Tool"

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top