如何使用数据库更改更新 edmx 文件? [英] How do you update an edmx file with database changes?

查看:60
本文介绍了如何使用数据库更改更新 edmx 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 edmx 文件,我更改了数据库中的一个表.我知道有一个从数据库更新模型"向导,但在很多情况下这是无用的.

I have an edmx file and I changed a table in my database. I know that there is an "Update Model from database" wizard, however in many cases this is useless.

例如,如果我将字段从非空更改为可为空,或者如果我删除字段,则更新模型不会反映更改.我必须删除实体并将其重新添加,以使更改显示在我的模型中.

For example if I change a field from non null to nullable or if I remove fields the update model does not reflect the changes. I have had to remove the entity and add it back in to get the changes to appear in my model.

根据以下问题:如何将数据库更改传播到我的 .edmx 文件?

其中一个答案似乎说的是同一件事,即您需要删除实体并将其重新添加.

One of the answers seems to say the same thing, that you need to remove the entity and add it back in.

这是确定的答案还是有更好的方法来做到这一点?

Is this the definitive answer or is there a better way to do this?

推荐答案

重要的第一步是准确了解使用更新模型向导时会发生什么.

An important first step is to understand exactly what happens when you use the update model wizard.

来自 MSDN Library:

ADO.NET 实体数据模型设计器(实体设计器)使用更新模型向导根据对数据库所做的更改来更新 .edmx 文件.作为此过程的一部分,更新模型向导会覆盖存储模型.更新模型向导还会对概念模型和映射进行一些更改,但仅在对象添加到数据库时才会进行这些更改.例如,当表被添加到数据库时,新的实体类型被添加到概念模型,当列被添加到表时,新的属性被添加到实体类型.有关对 .edmx 文件所做的更改的详细信息,请参阅 更新模型向导对 .edmx 文件所做的更改.

The ADO.NET Entity Data Model Designer (Entity Designer) uses the Update Model Wizard to update an .edmx file from changes made to the database. The Update Model Wizard overwrites the storage model as part of this process. The Update Model Wizard also makes some changes to the conceptual model and mappings, but it only makes these changes when objects are added to the database. For example, new entity types are added to the conceptual model when tables are added to the database, and new properties are added to entity types when columns are added to a table. For details about what changes are made to the .edmx file, see Changes Made to an .edmx File by the Update Model Wizard.

当您使用更新模型向导更新数据库时,它更新了 .edmx 文件中的存储模型,而不是概念模型.当对现有对象的定义进行更改时,仅更新存储模型;概念模型没有更新.有关更新模型向导所做更改的完整说明,请参阅上面的更新模型向导对 .edmx 文件所做的更改"链接.

When you updated the database using the update model wizard, it updated the storage model in the .edmx file and not the conceptual model. When changes are made to the definition of existing objects, only the storage model is updated; the conceptual model is not updated. For a complete description of changes that are made by the update model wizard, please see the "Changes Made to an .edmx File by the Update Model Wizard" link above.

以下是有关如何更新更新模型向导未更新的对象的一些选项(基于更改列定义的场景):

Here are some options on how to update objects that are not updated by the update model wizard (based on your scenario where a the column definition was altered):

  1. 使用更新模型向导(更新存储模型),使用设计器(默认)打开 .edmx 文件,找到所需的标量属性并在属性"窗口中编辑所需的属性.
  2. 使用更新模型向导(更新存储模型),使用 XML 编辑器打开 .edmx 文件,在 CSDL(概念模型)部分找到所需的属性并更改所需的属性.这与选项 1 基本相同,但您是直接编辑 XML(此处查找和替换可能很有用).
  3. 在模型浏览器中,从概念模型的实体类型部分中删除所需的实体,并从存​​储模型的表/视图部分中删除所需的表.然后使用更新模型向导将其添加回来.

最佳选择取决于给定的场景.例如,如果您只是更改了一列的定义,那么选项 1 可能是您的最佳选择.如果您更改了单个表中多个列的定义,则选项 3 可能是您的最佳选择.如果您更改了跨多个表使用的列(例如主键/外键),那么直接编辑 .edmx XML 可能是您的最佳选择.

The best option would depend on the given scenario. For example, if you just altered the definition of one column, then option 1 is likely you best choice. If you altered the definition of a number of columns in a single table, then option 3 might be your best choice. If you altered a column that is used across a number of tables (such as a primary / foreign key), then editing the .edmx XML directly might be your best option.

这篇关于如何使用数据库更改更新 edmx 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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