2

我正在使用 EntityFramework 4.3.1 并尝试运行这样的查询

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

当我运行查询并按预期返回 115 条记录时。但是当我查看记录时,它们都是一样的。所以我听探查器的查询,看看我错过了什么,看看下面的查询,它从管理工作室返回 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'

我认为问题出在 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();

第一个查询返回 115 行,第二个查询返回 1。

在此处输入图像描述

我错过了什么?

提前致谢。

4

1 回答 1

0

就像昆顿说我的模型配置是错误的。

如果你有超过 1 列作为键,你应该在上下文OnModelCreating方法上设置它们,比如modelBuilder.Entity<KullaniciYetkiView>().HasKey(ky => new { ky.KullaniciId, ky.LokasyonId, ky.YetkiId });

这里是 HasKey 方法的解释。http://msdn.microsoft.com/en-us/library/gg671266(v=vs.103).aspx

于 2013-01-07T16:02:57.313 回答