实体框架结果与数据库视图的差异

编程入门 行业动态 更新时间:2024-10-24 12:21:23
本文介绍了实体框架结果与数据库视图的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在我的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

更多推荐

实体框架结果与数据库视图的差异

本文发布于:2023-11-10 22:40:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1576663.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   实体   框架   差异   数据库

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!