我使用的是EF 6.1,我有一个3级深度继承,大约有12个类继承自一个名为BusinessEntity的基本抽象基类。简而言之,它看起来像这样:
public abstract class BusinessEntity
{
[Required, MaxLength(50)]
public virtual string Name { get; protected set; }
[Required]
public virtual long Id { get; protected set; }
...
}
然后我有Person类继承自:
[Table("Person")]
public class Person : BusinessEntity
{
// other properties here
}
然后我有另一个有属性Person的类:
public class Notification
{
[RequiredForAdd]
public virtual Person AddressedTo { get; protected set; }
}
当我查询通知DBSet时,如下所示:
var notifications = ctx.Notifications.Where(n => n.AddressedTo.Id == userId)
.Include(n => n.AddressedTo).ToList();
我看到这样的Sql:
exec sp_executesql N'SELECT
[Extent1].[NotificationType] AS [NotificationType],
[Extent1].[NotificationId] AS [NotificationId],
[Extent1].[EventData] AS [EventData],
[Extent1].[Message] AS [Message],
[Extent1].[NotifiedDate] AS [NotifiedDate],
[Extent1].[ReadDate] AS [ReadDate],
[Extent1].[CreatedDate] AS [CreatedDate],
[Extent1].[UpdatedDate] AS [UpdatedDate],
CASE WHEN ([Join1].[PersonStatus] IS NULL) THEN CAST(NULL AS varchar(1)) ELSE ''2X0X0X'' END AS [C1],
[Join1].[BusinessEntityId1] AS [BusinessEntityId],
[Join1].[EntityType] AS [EntityType],
[Join1].[Name] AS [Name],
[Join1].[DocumentMode] AS [DocumentMode],
[Join1].[Headline] AS [Headline],
[Join1].[LegacyReference] AS [LegacyReference],
[Join1].[IsDeleted] AS [IsDeleted],
[Join1].[CreatedDate] AS [CreatedDate1],
[Join1].[UpdatedDate] AS [UpdatedDate1],
[Join1].[Email] AS [Email],
[Join1].[FirstName] AS [FirstName],
[Join1].[OtherNames] AS [OtherNames],
[Join1].[LastName] AS [LastName],
[Join1].[Title] AS [Title],
[Join1].[DateOfBirth] AS [DateOfBirth],
[Join1].[PersonStatus] AS [PersonStatus],
[Join1].[WindowsName] AS [WindowsName],
[Join3].[CreatedBy_Id] AS [CreatedBy_Id],
[Join5].[Currency_Id] AS [Currency_Id],
[Join7].[UpdatedBy_Id] AS [UpdatedBy_Id],
CASE WHEN ([Join9].[BusinessEntityId2] IS NULL) THEN CAST(NULL AS varchar(1)) WHEN ((CASE WHEN ([Join9].[Discriminator] = N''Person'') THEN cast(1 as bit) ELSE cast(0 as bit) END) = 1) THEN ''2X0X'' ELSE ''2X0X0X'' END AS [C2],
[Join9].[BusinessEntityId2] AS [BusinessEntityId1],
[Join9].[EntityType] AS [EntityType1],
[Join9].[Name] AS [Name1],
[Join9].[DocumentMode] AS [DocumentMode1],
[Join9].[Headline] AS [Headline1],
[Join9].[LegacyReference] AS [LegacyReference1],
[Join9].[IsDeleted] AS [IsDeleted1],
[Join9].[CreatedDate] AS [CreatedDate2],
[Join9].[UpdatedDate] AS [UpdatedDate2],
[Join9].[Email] AS [Email1],
[Join9].[FirstName] AS [FirstName1],
[Join9].[OtherNames] AS [OtherNames1],
[Join9].[LastName] AS [LastName1],
[Join9].[Title] AS [Title1],
[Join9].[DateOfBirth] AS [DateOfBirth1],
[Join9].[PersonStatus] AS [PersonStatus1],
CASE WHEN ([Join9].[BusinessEntityId2] IS NULL) THEN CAST(NULL AS varchar(1)) WHEN ((CASE WHEN ([Join9].[Discriminator] = N''Person'') THEN cast(1 as bit) ELSE cast(0 as bit) END) = 1) THEN CAST(NULL AS varchar(1)) ELSE [Join9].[WindowsName] END AS [C3],
[Join11].[CreatedBy_Id] AS [CreatedBy_Id1],
[Join13].[Currency_Id] AS [Currency_Id1],
[Join15].[UpdatedBy_Id] AS [UpdatedBy_Id1],
[Extent1].[UpdatedBy_Id] AS [UpdatedBy_Id2]
FROM [dbo].[Notification] AS [Extent1]
LEFT OUTER JOIN (SELECT [Extent2].[BusinessEntityId] AS [BusinessEntityId1], [Extent2].[Email] AS [Email], [Extent2].[FirstName] AS [FirstName], [Extent2].[OtherNames] AS [OtherNames], [Extent2].[LastName] AS [LastName], [Extent2].[Title] AS [Title], [Extent2].[DateOfBirth] AS [DateOfBirth], [Extent2].[PersonStatus] AS [PersonStatus], [Extent2].[Discriminator] AS [Discriminator], [Extent2].[WindowsName] AS [WindowsName], [Extent3].[BusinessEntityId] AS [BusinessEntityId3], [Extent3].[EntityType] AS [EntityType], [Extent3].[Name] AS [Name], [Extent3].[DocumentMode] AS [DocumentMode], [Extent3].[Headline] AS [Headline], [Extent3].[LegacyReference] AS [LegacyReference], [Extent3].[IsDeleted] AS [IsDeleted], [Extent3].[CreatedDate] AS [CreatedDate], [Extent3].[UpdatedDate] AS [UpdatedDate]
FROM [dbo].[Person] AS [Extent2]
INNER JOIN (SELECT [Var_6].[BusinessEntityId] AS [BusinessEntityId], [Var_6].[EntityType] AS [EntityType], [Var_6].[Name] AS [Name], [Var_6].[DocumentMode] AS [DocumentMode], [Var_6].[Headline] AS [Headline], [Var_6].[LegacyReference] AS [LegacyReference], [Var_6].[IsDeleted] AS [IsDeleted], [Var_6].[CreatedDate] AS [CreatedDate], [Var_6].[UpdatedDate] AS [UpdatedDate]
FROM [dbo].[BusinessEntity] AS [Var_6]
WHERE [Var_6].[IsDeleted] <> 1 ) AS [Extent3] ON [Extent2].[BusinessEntityId] = [Extent3].[BusinessEntityId] ) AS [Join1] ON ([Join1].[Discriminator] = N''User'') AND ([Extent1].[CreatedBy_Id] = [Join1].[BusinessEntityId1])
LEFT OUTER JOIN (SELECT [Extent4].[BusinessEntityId] AS [BusinessEntityId4], [Extent4].[Discriminator] AS [Discriminator], [Extent5].[BusinessEntityId] AS [BusinessEntityId5], [Extent5].[CreatedBy_Id] AS [CreatedBy_Id]
FROM [dbo].[Person] AS [Extent4]
INNER JOIN (SELECT [Var_7].[BusinessEntityId] AS [BusinessEntityId], [Var_7].[CreatedBy_Id] AS [CreatedBy_Id]
FROM [dbo].[BusinessEntity] AS [Var_7]
WHERE [Var_7].[IsDeleted] <> 1 ) AS [Extent5] ON [Extent4].[BusinessEntityId] = [Extent5].[BusinessEntityId] ) AS [Join3] ON ([Join3].[Discriminator] = N''User'') AND ([Extent1].[CreatedBy_Id] = [Join3].[BusinessEntityId4])
LEFT OUTER JOIN (SELECT [Extent6].[BusinessEntityId] AS [BusinessEntityId6], [Extent6].[Discriminator] AS [Discriminator], [Extent7].[BusinessEntityId] AS [BusinessEntityId7], [Extent7].[Currency_Id] AS [Currency_Id]
FROM [dbo].[Person] AS [Extent6]
INNER JOIN (SELECT [Var_8].[BusinessEntityId] AS [BusinessEntityId], [Var_8].[Currency_Id] AS [Currency_Id]
FROM [dbo].[BusinessEntity] AS [Var_8]
WHERE [Var_8].[IsDeleted] <> 1 ) AS [Extent7] ON [Extent6].[BusinessEntityId] = [Extent7].[BusinessEntityId] ) AS [Join5] ON ([Join5].[Discriminator] = N''User'') AND ([Extent1].[CreatedBy_Id] = [Join5].[BusinessEntityId6])
LEFT OUTER JOIN (SELECT [Extent8].[BusinessEntityId] AS [BusinessEntityId8], [Extent8].[Discriminator] AS [Discriminator], [Extent9].[BusinessEntityId] AS [BusinessEntityId9], [Extent9].[UpdatedBy_Id] AS [UpdatedBy_Id]
FROM [dbo].[Person] AS [Extent8]
INNER JOIN (SELECT [Var_9].[BusinessEntityId] AS [BusinessEntityId], [Var_9].[UpdatedBy_Id] AS [UpdatedBy_Id]
FROM [dbo].[BusinessEntity] AS [Var_9]
WHERE [Var_9].[IsDeleted] <> 1 ) AS [Extent9] ON [Extent8].[BusinessEntityId] = [Extent9].[BusinessEntityId] ) AS [Join7] ON ([Join7].[Discriminator] = N''User'') AND ([Extent1].[CreatedBy_Id] = [Join7].[BusinessEntityId8])
LEFT OUTER JOIN (SELECT [Extent10].[BusinessEntityId] AS [BusinessEntityId2], [Extent10].[Email] AS [Email], [Extent10].[FirstName] AS [FirstName], [Extent10].[OtherNames] AS [OtherNames], [Extent10].[LastName] AS [LastName], [Extent10].[Title] AS [Title], [Extent10].[DateOfBirth] AS [DateOfBirth], [Extent10].[PersonStatus] AS [PersonStatus], [Extent10].[Discriminator] AS [Discriminator], [Extent10].[WindowsName] AS [WindowsName], [Extent11].[BusinessEntityId] AS [BusinessEntityId10], [Extent11].[EntityType] AS [EntityType], [Extent11].[Name] AS [Name], [Extent11].[DocumentMode] AS [DocumentMode], [Extent11].[Headline] AS [Headline], [Extent11].[LegacyReference] AS [LegacyReference], [Extent11].[IsDeleted] AS [IsDeleted], [Extent11].[CreatedDate] AS [CreatedDate], [Extent11].[UpdatedDate] AS [UpdatedDate]
FROM [dbo].[Person] AS [Extent10]
INNER JOIN (SELECT [Var_10].[BusinessEntityId] AS [BusinessEntityId], [Var_10].[EntityType] AS [EntityType], [Var_10].[Name] AS [Name], [Var_10].[DocumentMode] AS [DocumentMode], [Var_10].[Headline] AS [Headline], [Var_10].[LegacyReference] AS [LegacyReference], [Var_10].[IsDeleted] AS [IsDeleted], [Var_10].[CreatedDate] AS [CreatedDate], [Var_10].[UpdatedDate] AS [UpdatedDate]
FROM [dbo].[BusinessEntity] AS [Var_10]
WHERE [Var_10].[IsDeleted] <> 1 ) AS [Extent11] ON [Extent10].[BusinessEntityId] = [Extent11].[BusinessEntityId] ) AS [Join9] ON ([Join9].[Discriminator] IN (N''User'',N''Person'')) AND ([Extent1].[AddressedTo_Id] = [Join9].[BusinessEntityId2])
LEFT OUTER JOIN (SELECT [Extent12].[BusinessEntityId] AS [BusinessEntityId11], [Extent12].[Discriminator] AS [Discriminator], [Extent13].[BusinessEntityId] AS [BusinessEntityId12], [Extent13].[CreatedBy_Id] AS [CreatedBy_Id]
FROM [dbo].[Person] AS [Extent12]
INNER JOIN (SELECT [Var_11].[BusinessEntityId] AS [BusinessEntityId], [Var_11].[CreatedBy_Id] AS [CreatedBy_Id]
FROM [dbo].[BusinessEntity] AS [Var_11]
WHERE [Var_11].[IsDeleted] <> 1 ) AS [Extent13] ON [Extent12].[BusinessEntityId] = [Extent13].[BusinessEntityId] ) AS [Join11] ON ([Join11].[Discriminator] IN (N''User'',N''Person'')) AND ([Extent1].[AddressedTo_Id] = [Join11].[BusinessEntityId11])
LEFT OUTER JOIN (SELECT [Extent14].[BusinessEntityId] AS [BusinessEntityId13], [Extent14].[Discriminator] AS [Discriminator], [Extent15].[BusinessEntityId] AS [BusinessEntityId14], [Extent15].[Currency_Id] AS [Currency_Id]
FROM [dbo].[Person] AS [Extent14]
INNER JOIN (SELECT [Var_12].[BusinessEntityId] AS [BusinessEntityId], [Var_12].[Currency_Id] AS [Currency_Id]
FROM [dbo].[BusinessEntity] AS [Var_12]
WHERE [Var_12].[IsDeleted] <> 1 ) AS [Extent15] ON [Extent14].[BusinessEntityId] = [Extent15].[BusinessEntityId] ) AS [Join13] ON ([Join13].[Discriminator] IN (N''User'',N''Person'')) AND ([Extent1].[AddressedTo_Id] = [Join13].[BusinessEntityId13])
LEFT OUTER JOIN (SELECT [Extent16].[BusinessEntityId] AS [BusinessEntityId15], [Extent16].[Discriminator] AS [Discriminator], [Extent17].[BusinessEntityId] AS [BusinessEntityId16], [Extent17].[UpdatedBy_Id] AS [UpdatedBy_Id]
FROM [dbo].[Person] AS [Extent16]
INNER JOIN (SELECT [Var_13].[BusinessEntityId] AS [BusinessEntityId], [Var_13].[UpdatedBy_Id] AS [UpdatedBy_Id]
FROM [dbo].[BusinessEntity] AS [Var_13]
WHERE [Var_13].[IsDeleted] <> 1 ) AS [Extent17] ON [Extent16].[BusinessEntityId] = [Extent17].[BusinessEntityId] ) AS [Join15] ON ([Join15].[Discriminator] IN (N''User'',N''Person'')) AND ([Extent1].[AddressedTo_Id] = [Join15].[BusinessEntityId15])
WHERE ([Extent1].[AddressedTo_Id] = @p__linq__0) AND ([Extent1].[NotifiedDate] IS NULL)',N'@p__linq__0 bigint',@p__linq__0=1
似乎是为BusinessEntity的每个子类型创建一个Person表的连接,但实际上并没有加入其他子类型(为什么会这样)。
任何人都可以解释这种行为吗?