如果我有这个作为我的 TPT 模型:
public class Foo
{
public Int32 Id { get; set; }
public string Text { get; set; }
}
[Table("Bars")]
public class Bar : Foo
{
public string MoreText { get; set; }
}
和这样的派生 DbContext:
public class MyContext : DbContext
{
public DbSet<Foo> Foos { get; set; }
public DbSet<Bar> Bars { get; set; }
}
然后,当我对 Foo 执行查询时,最终的 SQL 将包含到 Bar 的外部连接。
例如:
uisng(var context = new MyContext())
{
Console.WriteLine(context.Foos.ToString());
}
将导致这作为最终的 SQL 语句
SELECT
CASE WHEN ( NOT (([Project1].[C1] = 1) AND ([Project1].[C1] IS NOT NULL))) THEN
'0X' ELSE '0X0X' END AS [C1],
[Extent1].[Id] AS [Id],
[Extent1].[Text] AS [Text],
CASE WHEN ( NOT (([Project1].[C1] = 1) AND ([Project1].[C1] IS NOT NULL))) THEN
CAST(NULL AS varchar(1)) ELSE [Project1].[MoreText] END AS [C2]
FROM [dbo].[Foos] AS [Extent1]
LEFT OUTER JOIN (SELECT
[Extent2].[Id] AS [Id],
[Extent2].[MoreText] AS [MoreText],
cast(1 as bit) AS [C1]
FROM [dbo].[Bars] AS [Extent2] ) AS [Project1] ON [Extent1].[Id] = [Proj
ect1].[Id]
我确实理解为什么 --- 这样做可以让我做类似的事情:
foreach(var x in context.Foos)
{
if(x is Bar) Console.WriteLine("Impressive");
else Console.WriteLine("Not so much");
}
但是,正如您可以想象的那样,这种对基类的查询会很快导致 SQL Server 处理的噩梦般的查询。因此问题。
是否可以通知 Linq 到 EF 5.0 它应该只带回基本类型而不是派生类型。因此使最终的 SQL 更简单?