我有这个SQL查询工作:
select sum(dbos.Points) as Points, dboseasons.Year
from dbo.StatLines dbos
inner join dbo.Games dbog on dbog.GameId = dbos.GameId
inner join dbo.Seasons dboseasons on dbog.Season_SeasonId = dboseasons.SeasonId
where dbos.PlayerId = 3
group by dboseasons.Year
它返回积分,年份(56,2016)
我正在尝试将此转换为Linq查询以与EF一起使用.
我有
var query =
from dbostats in _db.StatLines
join dbogames in _db.Games on dbostats.GameId equals dbogames.GameId
join dboseasons in _db.Seasons on dbogames.Season.SeasonId equals dboseasons.SeasonId
where dbostats.PlayerId == player.PlayerId
group dbostats.Points by dboseasons.Year into g
select new
{
Year = g.Key,
Points = g.Sum()
};
playerAndStatLines.StatLinesBySeason =
query
.ToList()
.Select( r => new StatsBySeason
{
Season = r.Year,
Points = r.Points
});
WHich返回空结果集.
当我查看它生成的SQL时,就是这样
SELECT
[GroupBy1].[K1] AS [Year],
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
[Extent3].[Year] AS [K1],
SUM([Extent1].[Points]) AS [A1]
FROM [dbo].[StatLines] AS [Extent1]
INNER JOIN [dbo].[Games] AS [Extent2] ON [Extent1].[GameId] = [Extent2].[GameId]
INNER JOIN [dbo].[Seasons] AS [Extent3] ON [Extent2].[Season_SeasonId] = [Extent3].[SeasonId]
WHERE ([Extent1].[Discriminator] IN (N'StatsBySeason',N'StatLines')) AND ([Extent1].[PlayerId] = 3)
GROUP BY [Extent3].[Year]
) AS [GroupBy1]
正如预期的那样,对我的数据库执行时返回一个空结果集.
这个问题似乎是这样的:
([Extent1].[Discriminator] IN (N'StatsBySeason',N'StatLines')) AND
如果我把它拿出并运行生成的查询,那么我会回到2016年,56结果.
什么是这个Extent1.Discriminator,为什么它从我的linq查询生成它?
我的MOdel课程:
public class PlayerAndStatLines
{
public PlayerWithTeam PlayerWithTeam { get; set; }
public IEnumerable<StatsBySeason> StatLinesBySeason { get; set; }
}
public class Season
{
public int SeasonId { get; set; }
public int Year { get; set; }
}
public class Game
{
public int GameId { get; set; }
public int HomeTeamId { get; set; }
public int AwayTeamId { get; set; }
public int HomeScore { get; set; }
public int AwayScore { get; set; }
public DateTime DatePlayed { get; set; }
public GameType GameType { get; set; }
public int? PlayoffGameNumber { get; set; }
public Season Season { get; set; }
}
public class StatLines
{
public int StatLinesId { get; set; }
public int GameId { get; set; }
public int PlayerId { get; set; }
public int TeamId { get; set; }
public int Points { get; set; }
public int DefensiveRebounds { get; set; }
public int OffensiveRebounds { get; set; }
public int Assists { get; set; }
public int Turnovers { get; set; }
public int Minutes { get; set; }
public int Steals { get; set; }
public int Blocks { get; set; }
public int Fouls { get; set; }
public int ThreePointFieldGoalsAttempted { get; set; }
public int ThreePointFieldGoalsMade { get; set; }
public int TwoPointFieldGoalsAttempted { get; set; }
public int TwoPointFieldGoalsMade { get; set; }
public int FreeThrowsMade { get; set; }
public int FreeThrowsAttempted { get; set; }
public bool Started { get; set; }
}
public class StatsBySeason : StatLines
{
public int Season { get; set; }
public string SeasonYears => Season + " / " + (Season + 1);
}
如果我运行以下SQL:
select Discriminator from dbo.StatLines
我得到两排,都是空的.
谢谢.
最佳答案 在同一个程序集中继承实体类时要非常小心(尽可能避免). EF发现派生类并决定您要使用
TPH inheritance strategy并静默(如果您使用自动迁移)创建并使用Discriminator列,当然对于现有数据是空的并且会破坏您的查询.
我看到两个选择:
>用继承替换继承:
public class StatsBySeason
{
public int Season { get; set; }
public StatLines StatLines { get; set; }
public string SeasonYears => Season + " / " + (Season + 1);
}
>让EF使用Fluent配置忽略StatsBySeason类:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Ignore<StatsBySeason>();
// ...
}
或数据注释:
[NotMapped]
public class StatsBySeason : StatLines
{
// ...
}