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

查看:236
本文介绍了如何使用实体框架6.1和MVC使用5 code首先从数据库后同步模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


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

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

我在蟾蜍DM专为SQL Server中现有的数据库模型,是非常重要的保持不断更新

I have a existing database model designed in Toad DM for SQL Server and is very important keep it always updated


  1. 使用ADO.NET实体数据模型,我选择的 code首先从数据库(适用于EF 6.1 新功能)生成的模型。注:型号类和successfuly产生的DbContext类,但生成的 NO的.edmx或.TT文件

  1. Using ADO.NET Entity Data Model I choose 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 add new scaffold item: MVC 5 Controllers with views, using Entity Framework. Note: Success, controllers and views generated

从现在开始,我不希望使用code首先来更新我的数据库。相反,我想根据数据库更改更新模型。接下来做什么?如果我没有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 a edmx file I'll not be able to update my model classes from database?

推荐答案

首先从数据库中的实体数据模型向导的code做了出色的工作创造你的实体类,好像他们是在code首先创建样式。如果有任何的方式来保持这些类先进的日期作为数据库的变化,类似于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首先向导来做到这一点:

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. 从数据库向导选项第一选择code

  3. 选择客户为对象增加(只是客户)及完成向导

  4. 打开原始文件的情况下,StoreDBContext.cs,并添加以下属性:
    public虚拟DbSet<客户>客户{搞定;设置;}

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

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首先从数据库后同步模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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