Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 5 and Visual Studio 2012 POCO Classes in Different Project

Tags:

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?

like image 219
CleverPatrick Avatar asked Sep 11 '12 17:09

CleverPatrick


People also ask

What is the use of POCO class in Entity Framework?

These classes (POCO classes) implements only the domain business logic of the Application. Some developers use Data Transfer Objects (DTOs) with the classes to pass the data between the layers because POCOs are also used to pass the data between the layers, but they become heavy.

What is Entity Framework reverse POCO generator?

The Reverse POCO Generator is a T4 template packaged as a Visual Studio extension allowing you to scaffold Entity Framework elements based on an existing database. It generates code such as DbContexts, entities, unit testable Fakes, stored procedures, and views just to name a few.


1 Answers

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.

like image 106
Philippe Avatar answered Sep 28 '22 15:09

Philippe