我启动了我的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 data1I 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
更多推荐
实体框架结果与数据库视图的差异
发布评论