实体框架5 - 如何从现有数据库生成POCO类 [英] Entity Framework 5 - How to generate POCO classes from existing database

查看:173
本文介绍了实体框架5 - 如何从现有数据库生成POCO类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS 2012和EF 5.我有一个现有的数据库,我想从现有的数据库创建POCO类。我按照步骤将ADO.NET实体数据模型添加到我的项目中。我通过向导来使用现有的数据库。然后在设计器打开时创建edmx和tt文件。但是,我想创建POCO对象并使用它们。 Microsoft站点声明POCO实体框架生成器已过时,我应该使用DBContext生成器。我无法找出我用来生成POCO类的步骤。我只看到edmx设计师。我真的不想要一个edmx文件,而只是POCO类。如何使用EF 5和VS 2012从现有数据库中获取POCO类?

解决方案

使用 EF 5.x DbContext流利生成器



您可以从在线模板添加:




  • 从现有数据库生成edmx

  • 选择添加新项目

  • 搜索POCO的在线模板

  • 添加EF 5.x DbContext流利生成器



它将在您的项目中添加三个T4模板:




  • XXX.Context.tt - 从DbContext继承的上下文

  • XXX.Entities.tt - POCO

  • XXX.Mappings.tt - 每个实体的流畅映射您必须手动设置您的edmx数据模型的路径,否则。每个模板都有行 string inputFile = @$ edmxInputFile $; 。您需要在此处提供您的edmx文件的名称:



    string inputFile = @Northwind.edmx; / p>

    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?

    解决方案

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

    这篇关于实体框架5 - 如何从现有数据库生成POCO类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆