获得ef(dbfirst)和Sql Query之间的差异结果

时间:2015-11-05 20:26:35

标签: database entity-framework entity

开始之前 我有View' PageViewModSession' 它的代码是

 SELECT        CONVERT(datetime, CONVERT(varchar(14), VisitStartDateTime) +    ':00:00') AS DateValue, MAX(dbo.PageLogGroupByDateTimeFull(VisitStartDateTime))  AS PageLogCount, 
                     (CASE MAX(dbo.SessionGroupByDateTimeFull(VisitStartDateTime)) WHEN 0 THEN 1 ELSE MAX(dbo.SessionGroupByDateTimeFull(VisitStartDateTime)) END) AS SessionLogCount, SiteInfoID
FROM            dbo.PageLog
GROUP BY CONVERT(varchar(14), VisitStartDateTime), SiteInfoID

并选择此视图时我的结果是 Sql Query Result.png

当使用此合成文件选择ef时

 var obj = db.PageViewModSessions.AsQueryable();

结果是
Ef Result.png

在结果

的每一行重复第一行

我在分析器中捕获创建的Sql查询查询

SELECT 
[Extent1].[DateValue] AS [DateValue], 
[Extent1].[PageLogCount] AS [PageLogCount], 
[Extent1].[SessionLogCount] AS [SessionLogCount], 
[Extent1].[SiteInfoID] AS [SiteInfoID]
FROM (SELECT 
[PageViewModSession].[DateValue] AS [DateValue], 
[PageViewModSession].[PageLogCount] AS [PageLogCount], 
[PageViewModSession].[SessionLogCount] AS [SessionLogCount], 
[PageViewModSession].[SiteInfoID] AS [SiteInfoID]
FROM [dbo].[PageViewModSession] AS [PageViewModSession]) AS [Extent1]

,结果是

2015-11-03 01:00:00.000        19        9        2
2015-11-03 02:00:00.000        19        4        2
2015-11-03 03:00:00.000        4        1        2
2015-11-03 11:00:00.000        7        5        2
2015-11-03 12:00:00.000        9        2        2
2015-11-04 01:00:00.000        1        1        2
2015-11-04 02:00:00.000        12        1        2
2015-11-04 03:00:00.000        5        1        2
2015-11-04 05:00:00.000        1        1        2
2015-11-04 06:00:00.000        4        1        2
2015-11-04 10:00:00.000        20        2        2
2015-11-04 11:00:00.000        19        4        2
2015-11-04 12:00:00.000        23        18        2
2015-11-05 02:00:00.000        1        1        2
2015-11-05 03:00:00.000        5        1        2
2015-11-05 04:00:00.000        25        2        2
2015-11-05 10:00:00.000        2        1        2
2015-11-05 11:00:00.000        3        1        2

为什么?!!

以及需要做些什么来解决这个问题

1 个答案:

答案 0 :(得分:0)

我处理这个 通过设置模型的两个键 DateValue和SiteInfoId