实体框架结果与数据库视图的差异 [英] Entity framework result discrepancy for a database views

查看:112
本文介绍了实体框架结果与数据库视图的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的DB中创建了一个特定视图(使用左连接加入约5-6个表)。该视图被添加到我的edmx(实体框架1.0)中。最近我注意到,使用edmx(linq到实体,然后ToList())获得的列记录之一在数据库视图中被重复了多次,不同的是

 列-N(DB视图的预期结果/结果)
---------
data1
data2
data3
data4
data5

列-N(实体框架生成的实际结果)
---------
data1
data1
data1
data1
data1

我启动了我的SQL分析器,得到我的应用程序发送到SQL Server的查询,运行它,并返回我的预期结果。



MSDN有一个类似的帖子 here 但主持人没有详细阐述了如何解决这个问题。我的密钥恰好是一个GUID


根本原因你指出我认为
是正确的,问题在
应用程序EF映射,因为EF具有
不同的对象映射规则与
数据库。当查询结果从数据库返回
时,EF
将根据自己设计的
逻辑执行应用程序
内存上的映射。



当您在数据库端设置
视图查询时,请务必考虑这些逻辑
。 I
认为你应该对
你的查看查询进行一些调整。



我不知道你是否已经排序了
的问题,如果没有,请提供
数据库结构相关的
问题和视图查询您有
写。



谢谢Binze


以前有人遇到过类似的问题吗?

解决方案>

问题其实是关键。您必须a)在视图中的每一行都有唯一的标识符。和b)在edmx中相应地映射该密钥。否则如你的引用所述,映射逻辑将看到每个后续的行和数据,它可以使用与


之前返回的相同的对象实例

I have one specific view created in my DB(joins about 5-6 tables with a left join).This view is added to my edmx (entity framework 1.0) . Recently I noticed that one of the column records obtained using the edmx (linq to entities and then ToList()) got duplicated multiple times though in the database view they were different

Column-N (Expected result/ result from DB view)
---------
data1
data2
data3
data4
data5

Column-N(Actual result generated by entity framework)
---------
data1
data1
data1
data1
data1

I fired up my SQL profiler,got the query which was sent by my application to the SQL Server, ran it and it returned me the expected result.

MSDN has a similar post here and here but the moderator has not elaborated on how to solve this problem. My key happens to be a GUID

The root cause you pointed out I think is correct, the problem is on the application side EF mapping, as EF has different object mapping rules with database. when the query results have been returned from database, the EF will do the mapping on application memory according to its own designed logic.

It's important to take these logic into account when you desingn your view query in your database side. I think you should do some adjustment on your view query.

I am not sure whether you have sorted the problem, if not please provide the database structure related to this issue and the view query you have written.

Thanks Binze

Has someone encountered a similar problem before ?

解决方案

The problem is in fact with the key. You have to a) have a unique identifier for each row in the view. and b) map that key accordingly in the edmx. Otherwise as your quote states, the mapping logic will see each subsequent row and figure that it can use the same object instance that it returned before

这篇关于实体框架结果与数据库视图的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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