15

I am using VS 2012 and EF 5. I have an existing database that I want to create POCO classes from the existing database. I followed the steps to add an ADO.NET Entity Data Model to my project. I went through the wizard to use an existing database. It then created the edmx and tt files with the designer open. However, I want to create the POCO objects and use them. The Microsoft site states that the POCO Entity Framework Generator is obsolete and I should use the DBContext Generator. I can't figure out steps I use to generate the POCO classes. I only see the edmx designer. I really don't even want an edmx file but instead just POCO classes. How can I get POCO classes to be created from an existing database using EF 5 and VS 2012?

user31673
  • 13,245
  • 12
  • 58
  • 96

2 Answers2

14

Use EF 5.x DbContext Fluent Generator

You can add it from online templates:

  • Generate edmx from existing database
  • Select Add New Item
  • Search online templates for POCO
  • Add EF 5.x DbContext Fluent Generator

It will add three T4 templates to your project:

  • XXX.Context.tt - context inherited from DbContext
  • XXX.Entities.tt - POCO entities
  • XXX.Mappings.tt - fluent mappings for each entity

BUT you need to setup path to your edmx data model manually. Each of these templates have line string inputFile = @"$edmxInputFile$";. You need to provide name of your edmx file here:

string inputFile = @"Northwind.edmx";

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • Thank you for the information. I have added the templates but no code has been generated. I altered all of them to fix the $edmxInputFile$ but nothing happens. I right clicked on each of them and clicked on Run Custom Tool. What am I missing? – user31673 Nov 11 '12 at 00:14
  • @user31673 actually you don't need any custom tool. Just save edited template, and it will automatically generate output file (it will be under node of template) – Sergey Berezovskiy Nov 11 '12 at 00:18
  • 1
    Ah, one more thing - delete context and entities generated with edmx file. You need only diagram. – Sergey Berezovskiy Nov 11 '12 at 00:29
  • Do you know how to delete this menu from VS? I have added this option and now I want to delete it because this menu appears in every MVC project. – Tomas Jan 21 '13 at 14:36
  • @Tomas what menu are you talking about? – Sergey Berezovskiy Jan 21 '13 at 14:38
9

The process to do this is pretty streamlined now, it seems. The steps from the accepted answer are now easy to do from the EDMX designer itself. Basically,

  • Generate the model (edmx) from an existing database by adding ADO.NET Entity Data Model to the project (see here for more details),
  • and then

Open the .edmx file in the Entity Designer.

Right-click an empty area on the Entity Designer surface and point to Add Code Generation Item.

In the Add New Item dialog, select Online Templates and type DBContext in the Search Online Templates text box.

Select the appropriate version for your template (5.0, if you want to target the Entity Framework 5.0).

Click OK.

This will do all the work, apparently. The quoted instructions here refer to Online Templates as installing EF 5.x DbContext Fluent Generator is required. If you have it already installed, there is no need to search for it in the Online Templates but in the Installed Templates.

For more info you can check this page, section "To use the DbContext Generator Template to Generate Object Layer Code".

Community
  • 1
  • 1
Alen Siljak
  • 2,482
  • 2
  • 24
  • 29