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

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

问题描述

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



例如,如果我将一个字段从非空值更改为可空,或如果我删除字段更新模型不反映更改。我不得不删除该实体并将其重新添加,以使更改显示在我的模型中。



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



其中一个答案似乎说同样的事情,你需要删除实体并将其添加回来。



这是一个明确的答案还是有更好的方法呢?

解决方案

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



MSDN库


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


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



以下是有关如何更新更新模型向导未更新的对象的一些选项(根据您更改列定义的方案):


  1. 使用更新模型向导(更新存储模型),使用设计器(默认)打开.edmx文件,找到所需的标量属性并在属性窗口中编辑所需的属性。

  2. 使用更新模型向导(更新存储模型),使用XML编辑器打开.edmx文件,在CSDL(概念模型)部分找到所需的属性,并更改所需的属性。这与选项1基本相同,但是您直接编辑XML(在这里可以使用查找和替换)。

  3. 从模型浏览器中删除所需的实体实体类型部分的概念模型和所需的表从存储模型的表/视图部分。然后使用更新模型向导将其添加回来。

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


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.

Per the following question: How do I propagate database changes to my .edmx file?

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.

From the MSDN Library:

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.

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. Use the update model wizard (to update the storage model), open the .edmx file using the designer (default), find the desired scalar property and edit the desired properties in the Properties windows.
  2. Use the update model wizard (to update the storage model), open the .edmx file using the XML editor, find the desired property in the CSDL (conceptual model) section and change the desired attributes. This is basically the same as option 1, but you're editing the XML directly (a find and replace might be useful here).
  3. From the Model Browser, delete the desired entity from the Entity Types section of the conceptual model and the desired table from the Tables / Views section of the storage model. Then use the update model wizard to add it back.

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天全站免登陆