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

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

问题描述

我在我的数据库中创建了一个特定视图(使用左连接连接大约 5-6 个表).此视图已添加到我的 edmx(实体框架 1.0)中.最近我注意到使用 edmx(linq to entity 然后是 ToList())获得的列记录之一被多次复制,尽管在数据库视图中它们是不同的

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

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

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 有一个类似的帖子 此处此处 但版主并没有详细说明如何解决这个问题.我的密钥恰好是一个 GUID

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

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

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.

谢谢彬泽

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

Has someone encountered a similar problem before ?

推荐答案

问题其实出在key上.您必须 a) 视图中的每一行都有一个唯一标识符.b) 在 edmx 中相应地映射该键.否则,正如您的引用所述,映射逻辑将看到每个后续行并指出它可以使用它之前返回的相同对象实例

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