[英]Entity Framework query result
I'm using EntityFramework 4.3.1 and trying to run a query like this 我正在使用EntityFramework 4.3.1并尝试运行这样的查询
Context.Set<KullaniciYetkiView>().Where(y => y.KullaniciId == kullaniciId && y.YetkiKod == yetkiKod)
When i run the query and it returns 115 records as expected. 当我运行查询时,它按预期返回115条记录。 But when i look the record all of them is same.
但是当我查看记录时,所有记录都是相同的。 So i listen the query from profiler for to look what am i missing, is see the below query and its return 115 different records from management studio.
因此,我侦听了探查器的查询,以查找我缺少的内容,请参阅以下查询,并从Management Studio返回115条不同的记录。
exec sp_executesql N'SELECT
[Extent1].[YetkiKod] AS [YetkiKod],
[Extent1].[KullaniciId] AS [KullaniciId],
[Extent1].[LokasyonId] AS [LokasyonId],
[Extent1].[YetkiId] AS [YetkiId],
[Extent1].[HiyerarsikKod] AS [HiyerarsikKod],
[Extent1].[LokasyonSeviye] AS [LokasyonSeviye],
[Extent1].[Yetkili] AS [Yetkili],
[Extent1].[Engelli] AS [Engelli],
[Extent1].[LokasyonEngelli] AS [LokasyonEngelli]
FROM [dbo].[sayKullaniciYetkiView] AS [Extent1]
WHERE ([Extent1].[KullaniciId] = @p__linq__0) AND ([Extent1].[YetkiKod] = @p__linq__1)',N'@p__linq__0 uniqueidentifier,@p__linq__1 nvarchar(4000)',@p__linq__0='283CCB41-3BDF-4BEF-BD26-E46191CA069D',@p__linq__1=N'FIN.SATISFATURA.E'
I think the problem is in EF and to prove it I run the code like this 我认为问题出在EF,为了证明这一点,我运行了这样的代码
var yetkiler1 = Context.Set<KullaniciYetkiView>().Where(y => y.KullaniciId == kullaniciId && y.YetkiKod == yetkiKod).Distinct().ToList();
var yetkiler2 = Context.Set<KullaniciYetkiView>().Where(y => y.KullaniciId == kullaniciId && y.YetkiKod == yetkiKod).ToList().Distinct();
First query returns 115 rows and the second returns 1. 第一个查询返回115行,第二个查询返回1。
What am I missing? 我想念什么?
Thanks in advance. 提前致谢。
Like Quinton said my model configuration is wrong. 就像Quinton所说的那样,我的模型配置是错误的。
if you have more than 1 column as key you should set them on contexts OnModelCreating method like modelBuilder.Entity<KullaniciYetkiView>().HasKey(ky => new { ky.KullaniciId, ky.LokasyonId, ky.YetkiId });
如果您有1列以上的键,则应在诸如
modelBuilder.Entity<KullaniciYetkiView>().HasKey(ky => new { ky.KullaniciId, ky.LokasyonId, ky.YetkiId });
等上下文的OnModelCreating方法上进行设置modelBuilder.Entity<KullaniciYetkiView>().HasKey(ky => new { ky.KullaniciId, ky.LokasyonId, ky.YetkiId });
Here is the HasKey method explanation. 这是HasKey方法的说明。 http://msdn.microsoft.com/en-us/library/gg671266(v=vs.103).aspx
http://msdn.microsoft.com/zh-CN/library/gg671266(v=vs.103).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.