EF中的POCO类别未按预期工作 [英] POCO Class in EF not working as Expected

查看:92
本文介绍了EF中的POCO类别未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用SQL创建了一个数据库,并在Visual Studio 2012中创建了一个EDMX.它会自动创建POCO(TT)类.一切看起来都很好.

I have created a DataBase in SQL and created an EDMX in Visual Studio 2012. It automatically created POCO (TT) classes. Everything looks fine.

现在,我更改表的列名.我更新了EDMX.用XML打开EDMX,一切看起来都很好.

Now I change the column name of a table. I update the EDMX. Opening the EDMX in XML and everything looks fine.

在TT中运行自定义"工具后,我看到另外创建了一个新属性,例如:

After I ran Custom tool in TT, I see that a new property got created additionally, e.g.:

SQL table name : Student

Column name : sName

在我的POCO课堂上

public int sName{ get; set; }

自动创建.

现在,我将SQL中的列名更改为

Now I change the column name in SQL to

Column name : studentName

我的POCO课

public int sName{ get; set; }

public int studentName{ get; set; }

这是一个错误还是我需要做一些事情来解决这个问题?

Is this a bug or do I need to do something to fix this?

我应该怎么做才能避免这种情况?

What should I do to avoid this?

此外,如果我更改任何SQL列的数据类型并从EDMX设计器中的数据库更新模型,则不会更新概念模型.我该怎么办?

Also, if I change the data type of any SQL column and update the model from the DB in my EDMX designer, the Conceptual Model is not updated. How do I go about this?

推荐答案

首先,要了解您的问题,您需要知道的是

First, to understand your problem, what you need to know is that the EDMX file is just an XML file that contains 3 different sections:

  • CSDL:概念模式定义语言
  • SSDL:存储架构定义语言
  • MSL:映射规范语言

CSDL包含组成您的概念模型的实体和关系. SSDL描述了您的数据库模型,而MSL是2之间的映射.

The CSDL contains the entities and relationships that make up your conceptual model. The SSDL describes your DB model and the MSL is the mapping between the 2.

从数据库更新模型"过程将更新SSDL(更改与当前数据库模式不一致的所有内容),只有在您向数据库模式中添加了新内容的情况下,它才会修改CSDL.

The "Update Model From DB" process will update the SSDL (change everything that is inconsistent with the current DB schema), it will only modify the CSDL in case you’ve added new things to your DB schema.

这是很正常的行为,因为您的概念模式可能/应该与数据库模式不同(除非您希望域模型看起来完全像数据库模型,这显然不符合OOP/DDD最佳实践).

This is quite a normal behavior since your Conceptual schema may/should differ from your DB schema (unless you want your Domain model to look exactly like a DB model which obviously do not sound as OOP/DDD best practices).

对于@Peru,解决方案是删除相关实体(而不是整个EDMX!),然后执行从数据库更新模型"过程.

As for @Peru, the solution would be to delete the concerned entity (not the entire EDMX!) and then perform the "Update Model From DB" process.

希望这会有所帮助!

有一个不是免费的工具,它是一个Visual Studio插件,允许您在CSDL和SSDL文件上应用对数据库所做的更改:

There's a tool, not for free, which is a Visual Studio plugin that allows you apply changes made into the DB, both on the CSDL and SSDL files: Huagati DBML/EDMX Tools. The only "free" solution is deleting the entity (or the right field within this entity) that needs to be updated.

请记住,CSDL应该由开发人员维护,并且必须看起来像对象模型而不是数据库模型.想象一下,您在实体之间具有设置继承关系,或者已将1个DB表拆分为2个EDMX实体,运行从DB更新模型"应该不会覆盖所有内容!

Remember that CSDL is supposed to be maintained by developers and must look like an Object Model rather than a DB Model. Imagine you have setup inheritance between your entities or that you have split 1 DB table to 2 EDMX entities, running "Update model from DB" should not overwrite everything!

这篇关于EF中的POCO类别未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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