2

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.

enter image description here

What am I missing?

Thanks in advance.

3
  • There seems to be something fishy with that KullaniciId column. The generated query treats it as a unique ID, yet it returns 115 records? Commented Dec 10, 2012 at 8:56
  • 1
    It may be a problem with your model config. Check out stackoverflow.com/a/7955773/1099260. Commented Dec 10, 2012 at 8:57
  • Yes, and it must return 115 records. Commented Dec 10, 2012 at 8:57

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.