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

喜夏-厌秋 提交于 2019-11-28 21:26:49

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!