linq-to-sql – 实体框架结果与数据库视图的差异

前端之家收集整理的这篇文章主要介绍了linq-to-sql – 实体框架结果与数据库视图的差异前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的数据库中创建了一个特定的视图(连接约5-6个表与左连接).这个视图被添加到我的edmx(实体框架1.0).最近我注意到,使用edmx( linq到entity,然后ToList())获得的列记录之一被重复了多次,尽管在数据库视图中它们是不同的
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的查询,运行它,并将其返回给我预期的结果.

MSDN有一个类似的帖子herehere,但是主持人还没有详细说明如何解决这个问题.我的密钥恰好是一个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

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

解决方法

问题其实是关键.您必须a)在视图中的每一行都有唯一的标识符.和b)在edmx中相应地映射该密钥.否则如您的引用所述,映射逻辑将看到每个后续行和数据,它可以使用与之前返回的相同的对象实例
原文链接:https://www.f2er.com/mssql/81884.html

猜你在找的MsSQL相关文章