Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 5 - How to generate POCO classes from existing database

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?

like image 374
user31673 Avatar asked Nov 10 '12 23:11

user31673


People also ask

How do you create a model and context class from an existing database?

To use code-first for an existing database, right click on your project in Visual Studio -> Add -> New Item.. Select ADO.NET Entity Data Model in the Add New Item dialog box and specify the model name (this will be a context class name) and click on Add. This will open the Entity Data Model wizard as shown below.

How do you create a model class from a database?

Right-click the App_Data folder in the Solution Explorer window and select the menu option Add, New Item. From the Add New Item dialog box, select SQL Server Database, give the database the name MoviesDB. mdf, and click the Add button.


1 Answers

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";

like image 55
Sergey Berezovskiy Avatar answered Sep 23 '22 01:09

Sergey Berezovskiy