实体框架。查看返回重复记录 [英] Entity Framework. View return duplicate records
问题描述
var data = this.context.vwRevenues
.Where(x => x。收入> = 0);
.OrderByDescending(x => x.year)
.ThenByDescending(x => x.month)
.Take(10)
.ToList();
此查询返回一组实体,但第一个实体等于5。
data [0] == data [4] // true
pre>
我从sql跟踪器获取此查询的sql脚本,并将其运行到SQL Management Studio中,
返回不同的记录。解决方案根据 @Giovane 答案
在我们的系统中,与实体框架处理视图相同的问题。尝试使用ROW_NUMBER()OVER()SQL创建一个具有唯一值的列,但不起作用。
我做了同样的事情,但是使其工作,我需要打开EDMX模型,然后选择此列作为实体密钥。
然后它将工作
有一篇非常好的文章这个
文章最重要的一行是:
在实体模型中包含视图时,该模型似乎只是使用第一个不可空列作为主键(如所有列在主键中使用应该不可为空)。
I use Entity Framework that contains view. And I have query:
var data = this.context.vwRevenues .Where(x => x.revenue >= 0); .OrderByDescending(x => x.year) .ThenByDescending(x => x.month) .Take(10) .ToList();
This query returns set of entities, but 1st entity equals 5th.
data[0] == data[4] // true
I take sql script for this query from sql tracer and run it into SQL Management Studio, it returns different records.
解决方案As per @Giovane Answer's
We had the same problem in our system with Entity Framework dealing with views. Try using ROW_NUMBER () OVER () SQL to create a column with unique values, but did not work.
I did the same thing but to make it work i need to open the EDMX model and then select a this column as an Entity Key.
Then it will work
There is a very good article on this
The articles most important line is:
When including a view in your Entity Model, the model seems to simply use the first not-nullable columns as primary key (as all columns used in the primary key should be non-nullable).
这篇关于实体框架。查看返回重复记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!