简体   繁体   中英

Entity Framework query result

I'm using EntityFramework 4.3.1 and trying to run a query like this

Context.Set<KullaniciYetkiView>().Where(y => y.KullaniciId == kullaniciId && y.YetkiKod == yetkiKod)

When i run the query and it returns 115 records as expected. 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.

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

        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.

在此处输入图片说明

What am I missing?

Thanks in advance.

Like Quinton said my model configuration is wrong.

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 });

Here is the HasKey method explanation. http://msdn.microsoft.com/en-us/library/gg671266(v=vs.103).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM