21

In VS 2010 and EF 4.4, you were able to move and edit .tt files when using the DBContext generator in Entity Framework such that your POCO objects where in a different project than your DBContext files.

See Here and Here for examples of what I am talking about.

In VS2012 / EF5 this seems not to be possible. the POCO classes are generated as a subitem under the EDMX file. The files cannot be copied from within Visual Studio. Moving the files from Explorer does not help because the files you moved get recreated at compilation time.

Am I missing something basic here?

I am not using any code generation items with EF5 (whereas I was with EF4.x.) Could that be the difference?

Community
  • 1
  • 1
CleverPatrick
  • 9,261
  • 5
  • 63
  • 86

3 Answers3

64

In Visual Studio 2012, when you add an ADO.NET Entity Data Model (*.edmx), it includes the T4 templates for the context and model classes as sub-items of the EDMX file. For example, if you add MyModel.edmx, it will have 4 sub-items as follows:

  1. MyModel.Context.tt
  2. MyModel.Designer.cs (in C# projects)
  3. MyModel.edmx.diagram
  4. MyModel.tt

MyModel.tt generates the POCO entities as sub-items. To generate the entities in a separate project, follow the following steps:

  1. Create a separate class project.
  2. Add new item, choose the "EF 5.x DbContext Generator" template. This creates the *.tt file. For example MyModel.tt.
  3. Edit the template file as follows:

    const string inputFile = @"MyModel.edmx"; // old value (remove)
    const string inputFile = @"..\MyOtherProjectName\MyModel.edmx"; // new value
    
  4. In your other project, expand the EDMX file and right-click on MyModel.tt, select Delete.

That's it. You're done. You now have your model and context in one project and the entities in a separate project.

Philippe
  • 4,088
  • 4
  • 44
  • 49
  • 1
    Very awesome, I had the same question. – Nick N. Mar 06 '13 at 19:58
  • To get this to work I have to add the absolute path, I cannot use "..\", anyone have any ideas? – jr3 Oct 17 '13 at 12:31
  • @jr3, is your project containing the model EDMX a sibling project or is its location in a different hierarchy? Please provide both projects absolute paths and I'm sure we can figure out the relative path. – Philippe Oct 17 '13 at 17:33
  • @Philippe, Yes the projects are both under the same solution when using relative path notation I get file not found. However as soon as I plug in the absolute path to the EDMX in the other project it works. EDIT: Here are my paths: http://pastebin.com/P51s8PeU – jr3 Oct 17 '13 at 18:05
  • @jr3, your template is in a folder inside the project, so you need 2 "..\" to go up twice. See http://pastebin.com/M6ZgqLNn – Philippe Oct 17 '13 at 19:31
  • @Philippe Can we move Context too, in the Project where POCO Entities Reside? – Sana.91 Oct 14 '14 at 06:12
6

Check out the following post: Visual Studio 2012 - Can't move EF .tt files

It speaks to how you can remove the dependency information of the .tt file to the .edmx file within the assoicated .csproj file. This will then allow you to drag the .tt file from within Solution Explorer.

Just make sure to update the file path in the begining of the .tt file to point to the .edmx as described in the previous answer and shown below:

const string inputFile = @"..\EFTest\EFTestModel.edmx";

There are actual several pieces and steps and missing any single one can prevent the separation of the POCO classes from working correctly. I created a blog post that details the entire process that you can view below:

Separating Entity Framework POCO Classes Generated From T4 Template in VS.NET 2012:
http://allen-conway-dotnet.blogspot.com/2013/01/separating-entity-framework-poco.html

Community
  • 1
  • 1
atconway
  • 20,624
  • 30
  • 159
  • 229
  • I had a similar problem though I also had to edit some of the entries in the project file too. See: http://stackoverflow.com/questions/12200258/visual-studio-2012-cant-move-ef-tt-files/16216001#16216001 – GrandMasterFlush Apr 25 '13 at 13:29
2

So, you DO have to move it through windows Explorer now. And then edit the path to the EDMX file in the .tt file you moved. Once you do that it works. (I know I say it does not above, but I must have done something wrong the first time I tried.)

CleverPatrick
  • 9,261
  • 5
  • 63
  • 86