简体   繁体   English

实体框架6从现有表创建模型

[英]Entity Framework 6 Create model from existing table

We have to maintain a very old application. 我们必须维护一个非常古老的应用程序。 There is an old part (all data access is handled with stored procedures) and since my predecessors took over the application, they started to build a new part for the new requirements which uses entity framework 6. 有一个旧部分(所有数据访问都使用存储过程处理),并且由于我的前任接管了应用程序,他们开始为使用实体框架6的新需求构建新部件。

Now the customer has a new requirement that needs access to one of the tables for which there isn't an EF-model yet. 现在,客户有一个新的需求,需要访问其中一个没有EF模型的表。

Is there an easy way to automatically create an EF-model from an existing database table? 是否有一种从现有数据库表自动创建EF模型的简单方法? ( I only want this one table, not the whole remaining database! ) Do you know of any tool that can achieve this or do I have to write it by hand? 我只想要这一个表,而不是整个剩余的数据库! )你知道任何可以实现这个的工具,还是我必须手工编写?

Thanks in advance 提前致谢

Open the Entity Data Model (edmx) file (edit: or create one if you don't already have one), right click on the design surface in a blank area, and select "Update Model from Database". 打开实体数据模型(edmx)文件(编辑:或创建一个,如果您还没有),右键单击空白区域中的设计图面,然后选择“从数据库更新模型”。

Select the proper connection string if prompted, then choose the "Add" tab, and drill down to the tables in your database you want to add and put a check mark by them. 如果出现提示,请选择正确的连接字符串,然后选择“添加”选项卡,并深入查看要添加的数据库中的表,并按其添加复选标记。 Click Finish and you're done. 单击完成,您就完成了。

I use EF Power Tools which is a plugin for Visual Studio. 我使用的是EF Power Tools ,它是Visual Studio的插件。

Download here 在这里下载

With the plugin, you can reverse engineer your database in order to create your objects and mappings in your application. 使用该插件,您可以对数据库进行反向工程,以便在应用程序中创建对象和映​​射。 Despite the fact that it will save you a lot of time, note that it is not perfect. 尽管它会节省你很多时间,但请注意它并不完美。 You might want to review your indexes and relationships as some were missing in my case. 您可能希望查看索引和关系,因为我的案例中缺少某些索引和关系。

To Reverse engineer your database, it's as simple as this: 要反向设计数据库,它就像这样简单:

The Reverse Engineer Code First command is used to generate the POCO, mapping (configuration), and derived DbContext classes that are based on an existing database. Reverse Engineer Code First命令用于生成基于现有数据库的POCO,映射(配置)和派生的DbContext类。

  • Right click the project and select Entity Framework –> Reverse Engineer Code First . 右键单击该项目,然后选择Entity Framework - > Reverse Engineer Code First

  • Enter the information about an existing database based on which you want to reverse engineer Code First. 输入有关您希望对Code First进行反向工程的现有数据库的信息。

You can check this link from Microsoft for a complete example: 您可以从Microsoft查看此链接以获取完整示例:

https://msdn.microsoft.com/en-us/data/jj593170.aspx https://msdn.microsoft.com/en-us/data/jj593170.aspx

IMPORTANT: As of Entity Framework 7 (not released yet), only the Code First approach will be used. 重要提示:自实体框架7(尚未发布)起,仅使用Code First方法。 You might want to consider this before choosing another approach like Model First, for instance. 例如,在选择Model First之类的另一种方法之前,您可能需要考虑这一点。 You can read more about: EF7 - What Does “Code First Only” Really Mean 您可以阅读更多关于: EF7 - “仅代码优先”的含义

Check out the EF power tools plugin which give you context menus for Reverse Engineer Code First as well as other code generate options 查看EF电动工具插件,它为您提供Reverse Engineer Code First上下文菜单以及其他代码生成选项

https://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d https://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d

Can't you just create an EDMX and only have it select the tables you want it to point to instead of the whole DB? 你不能只创建一个EDMX,只让它选择你想要它指向的表而不是整个数据库吗?

Look at Generate From Database in this article 请参阅本文中的Generate From Database

https://msdn.microsoft.com/en-us/data/jj206878.aspx https://msdn.microsoft.com/en-us/data/jj206878.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Entity Framework中的现有表创建模型 - Creating model from existing table in Entity Framework 实体框架不会从 MVC 中的实体数据模型(来自现有数据库)创建上下文和表类(.cs) - Entity Framework doesn't create Context and table classes(.cs) from Entity Data Model (from existing database) in MVC 将不带键的现有表添加到实体框架模型 - Adding an existing table with no Key to an Entity Framework model 当我从现有数据库创建模型时,Entity Framework 4.0 生成只读模型 - Entity Framework 4.0 generating read only model when I create model from existing database 在实体框架中为单个表手动创建模型 - Manually create model for single table in Entity Framework 首先是实体框架代码-创建表并将现有表中的数据插入到新创建的表中, - Entity framework code first - create table and insert data from existing table into newly created one, 使用Entity Framework将模型映射到现有数据库表 - Map model to existing database table using Entity Framework 如何在现有的远程数据库中添加表并将其链接到Entity Framework模型? - How to add table in existing remote database and link it to Entity Framework model? 实体框架 - 从视图创建模型? - Entity Framework - Create a model from a view? 尝试从Visual Studio 2015中的现有表创建实体数据模型时出现错误 - Getting error when trying to create Entity Data Model from existing table in Visual Studio 2015
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM