从摘要视图中选择时,EF4返回不正确的值 [英] EF4 returns incorrect values when selecting from summary view

查看:125
本文介绍了从摘要视图中选择时,EF4返回不正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EF4从视图中读取数据。这是一个由group by组成的摘要视图,只读 - 它没有主键。所以为了使它导入到EF4我使用经常建议的技巧 - 使用ISNULL(列,-11)作为视图中的第一列。这有助于EF4推断主键并导入视图。

I am using EF4 to read data from a view .This is a summary view with group by etc and is readonly-it does not have a primary key. So inorder to make it import into EF4 I used an often suggested trick -ie use ISNULL(column,-11) as the first column in the view. This helps EF4 to infer primary key and import the view.

然而,当我从EF4的视图中选择时,很奇怪的事情发生 - 与管理工作室中的视图直接选择相比,我收到的数据不正确。我不敢相信,所以我尝试了几次,错误仍然存​​在。只是为了确保我在SQL Server中创建了一个表,从视图中选择所有的表,并访问了EF4中的表。然后返回正确的数据!

However when I select from the view in EF4 very strange thing happens - I get incorrect data as compared to when I directly select from the view in Management studio. I could not believe this so I tried a few times and the error persisted. Just to make sure I created a table in SQL Server by select all from the view and accessed the table in EF4 .But then in returns correct data!

视图不是非常复杂,除了它是一个摘要视图(用关节组合),它使用SQL公用表表达式(即with子句)。

The view is not very complicated except that it is a summary view (group by with joints) and it uses a SQL Common table expression(ie the with clause).

When I select straight in database using select * from vw_responserate I get 

coltext       SurveyId  rowtext1    rowtext2            cnt
Anwender    2   Angestellte/r   Französische Schweiz    1
Anwender    2   Angestellte/r   Italienische Schweiz    1
IT Spezialist   2   Angestellte/r   Deutsche Schweiz    1
IT Spezialist   2   Mittleres Management / Senior Management    Italienische Schweiz    1

When I select from EF4 I get

coltext rowtext1 rowtext2 cnt
Anwender      Angestellte/r Französische Schweiz 1
Anwender      Angestellte/r Französische Schweiz 1
IT Spezialist Angestellte/r Deutsche Schweiz 1
IT Spezialist  Angestellte/r Deutsche Schweiz 1

任何帮助将赞赏

谢谢

推荐答案

我有同样的问题,是为视图定义的键不唯一地标识一行。

I had the same issue, the problem is that the key defined for the view does not uniquely identify a single row.

查看样本数据,您的问题应通过将coltext,rowtext1和rowtext2设置为关键。

Looking at the sample data, your problem should be solved by setting coltext, rowtext1 and rowtext2 as the key.

可以通过设计师右键单击每个属性并选择实体密钥来完成。

This can be done through the designer by right clicking each property and selecting 'Entity Key'.

这篇关于从摘要视图中选择时,EF4返回不正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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