使用 Entity Framework 6.1 和 MVC 5 从数据库中使用 Code First 后如何同步模型? [英] How to sync model after using Code First from Database using Entity Framework 6.1 and MVC 5?

查看:22
本文介绍了使用 Entity Framework 6.1 和 MVC 5 从数据库中使用 Code First 后如何同步模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 使用 EF 6.1、MVC 5、VS 2013、C#

  • Using EF 6.1, MVC 5, VS 2013, C#

我有一个在 Toad DM 中为 SQL Server 设计的现有数据库模型,保持更新非常重要

I have an existing database model designed in Toad DM for SQL Server and it's very important keep it always updated

  1. 使用 ADO.NET 实体数据模型我选择了Code First from Database(EF 6.1 中的新功能)来生成模型.注意:模型类和 DbContext 类生成成功,但没有生成 .edmx 或 .tt 文件.

  1. Using ADO.NET Entity Data Model I chose Code First from Database (new feature in EF 6.1) to generate the models. Note: Model classes and DbContext class generated successfuly but NO .edmx or .tt file was generated.

接下来我添加了一个新的脚手架项目:MVC 5 Controllers with views, using Entity Framework.注意:成功,控制器和视图生成

Next I added a new scaffold item: MVC 5 Controllers with views, using Entity Framework. Note: Success, controllers and views generated

问题

从现在开始,我不想使用 Code First 来更新我的数据库.相反,我希望根据数据库更改更新模型.接下来做什么?如果我没有 edmx 文件,我将无法从数据库更新我的模型类吗?

Question

From now on I don't want to use Code First to update my database. Instead I want the models to be updated based on database changes. What to do next? If I don't have an edmx file will I not be able to update my model classes from the database?

推荐答案

Entity Data Model Wizard 的 Code First from Database 在创建实体类方面做得非常出色,就好像它们是以 Code First 风格创建的一样.您要问的是,是否有任何方法可以使这些类在您的数据库更改时保持最新,类似于 EDMX 样式从数据库更新模型".根据我的研究,使用内置工具不可能.但是,这是我发现有用的一种解决方法:

The Entity Data Model Wizard's Code First from Database does an excellent job creating your entity classes, as if they were created in the Code First style. What you are asking is if there is any way to keep these classes up-to-date as your database changes, similar to the EDMX style "Update Model From Database". From what I've researched this is not possible using the built-in tooling. However, here is one workaround that I've found useful:

假设我有一个包含产品表和客户表的数据库.最初我创建了一个 StoreDBContext 类,并选择产品作为我的对象之一.现在我想将客户表作为新实体添加到现有上下文中.以下是使用 Code First 向导执行此操作的方法:

Let's say I have database with a product table and customer table. Originally I created a StoreDBContext class, and selected product as one of my objects. Now I want to add the customer table as a new entity to the existing context. Here's how to do this using the Code First Wizard:

  1. 创建一个新的实体数据模型,将其命名为 StoreDBContextTemp 或其他名称
  2. 先从数据库向导选项中选择代码
  3. 选择客户作为要添加的对象(仅客户)&完成向导
  4. 打开新创建的上下文文件 StoreDBContextTemp.cs,然后复制新添加实体的虚拟属性:
  1. Create a new Entity Data Model, call it StoreDBContextTemp or whatever
  2. Choose the code first from database wizard option
  3. Select customer as an object to add (just customer) & complete the wizard
  4. Open the newly created context file, StoreDBContextTemp.cs, and copy the virtual properties of your newly added entities:

公共虚拟 DbSet客户{get;设置;}

  1. 将这些新属性粘贴到您的原始 StoreDBContext.cs dbcontext 类中.
  2. 删除StoreDBContextTemp.cs,并删除app.config/web.confg等中StoreDBContextTemp的连接字符串.
  3. 您现在可以在 StoreDBContext 类上使用 Customer
  1. Paste these new properties into your Original StoreDBContext.cs dbcontext class.
  2. Delete StoreDBContextTemp.cs, and remove the connection string for StoreDBContextTemp in app.config/web.confg etc.
  3. You can now use Customer on the StoreDBContext class

如果您添加或删除表,您将需要手动调整字段,但至少您不需要在每次将新表添加到模型时手写几十个属性.

If you add or remove tables you will need to manually adjust fields, but at least you won't need to hand write dozens of properties each time a new table is added to the model.

这篇关于使用 Entity Framework 6.1 和 MVC 5 从数据库中使用 Code First 后如何同步模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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