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

查看:194
本文介绍了使用实体框架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实体数据模型我从数据库中选择了 (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控制器。注意:生成成功,控制器和视图

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?

推荐答案

实体数据模型向导的代码首先从数据库做一个很好的工作创建您的实体类,就好像它们是以代码优先样式创建的。您所要求的是,如果有任何方法可以使数据库更改保持这些类的最新状态,类似于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类,并选择了我的产品作为我的对象之一。现在我想将客户表添加为现有上下文中的一个新实体。以下是使用代码第一向导执行此操作:

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. 打开原始上下文文件StoreDBContext.cs,并添加以下属性:
    public virtual DbSet< Customer>客户{get; set;}

删除StoreDBContextTemp.cs,并在app.config / web.confg等中删除StoreDBContextTemp的连接字符串。

Delete StoreDBContextTemp.cs, and remove the connection string for StoreDBContextTemp in app.config/web.confg etc.

或删除您需要手动调整字段的表,但是至少每当新表添加到模型中时,您都不需要手写数十个属性。

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.

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

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