简体   繁体   English

重新生成实体框架代码优先模型

[英]Regenerate Entity Framework Code First Models

Using Entity Framework 6 Code First in an ASP.NET project with Visual Studio 2013, is there any way to rebuild (or update) the generated DataContext and model classes without stepping through the Entity Data Model Wizard every time?在带有 Visual Studio 2013 的 ASP.NET 项目中使用 Entity Framework 6 Code First,有没有办法重建(或更新)生成的 DataContext 和模型类,而无需每次都单步执行实体数据模型向导?

I'm fully aware of how to do this with an EDMX designer, but again, I'm using the "Code First from database" method and just wondering if there's a one-click (or one console command) way to trigger the rebuild without having to delete the generated context class and then step through the Entity Data Model Wizard every time I make a change to the backing database.我完全知道如何使用 EDMX 设计器来做到这一点,但同样,我正在使用“数据库中的代码优先”方法,只是想知道是否有一种一键(或一个控制台命令)方式来触发重建不必删除生成的上下文类,然后在每次更改后备数据库时单步执行实体数据模型向导。

Code first requires you to create the DataContext by hand.代码首先要求您手动创建 DataContext。 You don't create/change the database and refresh the DataContext class.您无需创建/更改数据库并刷新 DataContext 类。 The Code first from database or EF Reverse POCO template is a middle ground between true Code First and the Database First approach of doing things. Code first from database 或 EF Reverse POCO 模板是真正的 Code First 和 Database First 做事方法之间的中间地带。 It meets at the middle by generating the same kind of POCO classes that you would have written by hand in Code First.它通过生成与您在 Code First 中手动编写的相同类型的 POCO 类而在中间相遇。 They don't do it in Code First way but the end result is something similar to Code First.他们不是以 Code First 的方式来做的,但最终结果类似于 Code First。 Hence the confusing name.因此,令人困惑的名称。

If you are using one of these templates to generate POCOs, you can right click on the t4 template file and click Run Custom Tool.如果您使用这些模板之一来生成 POCO,您可以右键单击 t4 模板文件,然后单击运行自定义工具。 If that doesn't work, you might want to delete that entity and then run the custom tool again.如果这不起作用,您可能希望删除该实体,然后再次运行自定义工具。 Also right clicking on the EDMX and clicking 'Update Model from Database' should work.同样右键单击 EDMX 并单击“从数据库更新模型”应该可以工作。

In VS 2015 (and supposedly 2013/2012) you can use the Entity Framework Reverse POCO generator to accomplish this.在 VS 2015(以及 2013/2012)中,您可以使用实体框架反向 POCO 生成器来完成此操作。

https://visualstudiogallery.msdn.microsoft.com/ee4fcff9-0c4c-4179-afd9-7a2fb90f5838 https://visualstudiogallery.msdn.microsoft.com/ee4fcff9-0c4c-4179-afd9-7a2fb90f5838

You can make all your changes to database first, and to re-generate your models all you have to do is save your Database.tt file (usually I just add white space).您可以先对数据库进行所有更改,然后重新生成模型,您只需保存 Database.tt 文件(通常我只添加空格)。

I have the solution for rebuild without wizard:我有没有向导的重建解决方案:

using the t4 is Transform All T4 Templates and create the classes使用 t4 是转换所有 T4 模板并创建类

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM