EF:TPH用于多个继承类

时间:2016-03-16 12:28:07

标签: c# entity-framework linq inheritance tph

我有一个带有多个继承类的抽象类,确切地说是6。其中一个类引用了另一个类。 enter image description here 这是此设置的示例。 当我实现这个场景时,我在数据库中创建了一个这样的TPH表。但是当我尝试从这个TPH表中获取数据时,查询成本太高,以至于我的应用程序失去了与数据库的连接。

LINQ查询是这样的:

await context.Animals
.Where(t => getTheseIds.Contains(t.Owner1_FK) 
|| getTheseIds.Contains(t.Owner2_FK)).ToListAsync();

Entity Framework创建的SQL查询是一个庞大的混乱,包含每个继承类的大量CASE WHEN。 过滤器(WHERE)子句占此查询总成本的82%。

SQL Profiler中的SQL转储

    SELECT 
    [Extent1].[ID] AS [ID], 
    CASE WHEN ([Extent1].[Discriminator] = N'Cat') THEN '0X0X' WHEN ([Extent1].[Discriminator] = N'GoldFish') THEN '0X1X' WHEN ([Extent1].[Discriminator] = N'Dog') THEN '0X2X' WHEN ([Extent1].[Discriminator] = N'Parrot') THEN '0X3X' WHEN ([Extent1].[Discriminator] = N'Turtle') THEN '0X4X' ELSE '0X5X' END AS [C1], 
    [Extent1].[Active] AS [Active], 
    [Extent1].[DepreciationStart] AS [DepreciationStart], 
    [Extent1].[DepreciationStartExtraYear] AS [DepreciationStartExtraYear], 
    [Extent1].[DepreciationStartGivenYear] AS [DepreciationStartGivenYear], 
    [Extent1].[EndRule] AS [EndRule], 
    CASE WHEN ([Extent1].[Discriminator] = N'Cat') THEN [Extent1].[NumberOfYears] WHEN ([Extent1].[Discriminator] = N'GoldFish') THEN CAST(NULL AS int) WHEN ([Extent1].[Discriminator] = N'Dog') THEN CAST(NULL AS int) WHEN ([Extent1].[Discriminator] = N'Parrot') THEN CAST(NULL AS int) WHEN ([Extent1].[Discriminator] = N'Turtle') THEN CAST(NULL AS int) END AS [C2], 
    CASE WHEN ([Extent1].[Discriminator] = N'Cat') THEN [Extent1].[PercentsCommaSeparatedDouble] WHEN ([Extent1].[Discriminator] = N'GoldFish') THEN CAST(NULL AS varchar(1)) WHEN ([Extent1].[Discriminator] = N'Dog') THEN CAST(NULL AS varchar(1)) WHEN ([Extent1].[Discriminator] = N'Parrot') THEN CAST(NULL AS varchar(1)) WHEN ([Extent1].[Discriminator] = N'Turtle') THEN CAST(NULL AS varchar(1)) END AS [C3], 
    CASE WHEN ([Extent1].[Discriminator] = N'Cat') THEN CAST(NULL AS float) WHEN ([Extent1].[Discriminator] = N'GoldFish') THEN [Extent1].[Percent] WHEN ([Extent1].[Discriminator] = N'Dog') THEN CAST(NULL AS float) WHEN ([Extent1].[Discriminator] = N'Parrot') THEN CAST(NULL AS float) WHEN ([Extent1].[Discriminator] = N'Turtle') THEN CAST(NULL AS float) END AS [C4], 
    CASE WHEN ([Extent1].[Discriminator] = N'Cat') THEN CAST(NULL AS float) WHEN ([Extent1].[Discriminator] = N'GoldFish') THEN [Extent1].[PercentStart] WHEN ([Extent1].[Discriminator] = N'Dog') THEN CAST(NULL AS float) WHEN ([Extent1].[Discriminator] = N'Parrot') THEN CAST(NULL AS float) WHEN ([Extent1].[Discriminator] = N'Turtle') THEN CAST(NULL AS float) END AS [C5], 
    CASE WHEN ([Extent1].[Discriminator] = N'Cat') THEN CAST(NULL AS float) WHEN ([Extent1].[Discriminator] = N'GoldFish') THEN [Extent1].[RemainingAmount] WHEN ([Extent1].[Discriminator] = N'Dog') THEN CAST(NULL AS float) WHEN ([Extent1].[Discriminator] = N'Parrot') THEN CAST(NULL AS float) WHEN ([Extent1].[Discriminator] = N'Turtle') THEN CAST(NULL AS float) END AS [C6], 
    CASE WHEN ([Extent1].[Discriminator] = N'Cat') THEN CAST(NULL AS int) WHEN ([Extent1].[Discriminator] = N'GoldFish') THEN CAST(NULL AS int) WHEN ([Extent1].[Discriminator] = N'Dog') THEN [Extent1].[ActiveExtendedProfile] WHEN ([Extent1].[Discriminator] = N'Parrot') THEN CAST(NULL AS int) WHEN ([Extent1].[Discriminator] = N'Turtle') THEN CAST(NULL AS int) END AS [C7], 
    CASE WHEN ([Extent1].[Discriminator] = N'Cat') THEN CAST(NULL AS int) WHEN ([Extent1].[Discriminator] = N'GoldFish') THEN CAST(NULL AS int) WHEN ([Extent1].[Discriminator] = N'Dog') THEN [Extent1].[NumberOfYears1] WHEN ([Extent1].[Discriminator] = N'Parrot') THEN CAST(NULL AS int) WHEN ([Extent1].[Discriminator] = N'Turtle') THEN CAST(NULL AS int) END AS [C8], 
    CASE WHEN ([Extent1].[Discriminator] = N'Cat') THEN CAST(NULL AS varchar(1)) WHEN ([Extent1].[Discriminator] = N'GoldFish') THEN CAST(NULL AS varchar(1)) WHEN ([Extent1].[Discriminator] = N'Dog') THEN [Extent1].[ValuesCSDouble] WHEN ([Extent1].[Discriminator] = N'Parrot') THEN CAST(NULL AS varchar(1)) WHEN ([Extent1].[Discriminator] = N'Turtle') THEN CAST(NULL AS varchar(1)) END AS [C9], 
    CASE WHEN ([Extent1].[Discriminator] = N'Cat') THEN CAST(NULL AS float) WHEN ([Extent1].[Discriminator] = N'GoldFish') THEN CAST(NULL AS float) WHEN ([Extent1].[Discriminator] = N'Dog') THEN CAST(NULL AS float) WHEN ([Extent1].[Discriminator] = N'Parrot') THEN [Extent1].[Percent1] WHEN ([Extent1].[Discriminator] = N'Turtle') THEN CAST(NULL AS float) END AS [C10], 
    CASE WHEN ([Extent1].[Discriminator] = N'Cat') THEN CAST(NULL AS int) WHEN ([Extent1].[Discriminator] = N'GoldFish') THEN CAST(NULL AS int) WHEN ([Extent1].[Discriminator] = N'Dog') THEN CAST(NULL AS int) WHEN ([Extent1].[Discriminator] = N'Parrot') THEN [Extent1].[NumberOfYears2] WHEN ([Extent1].[Discriminator] = N'Turtle') THEN CAST(NULL AS int) END AS [C11], 
    CASE WHEN ([Extent1].[Discriminator] = N'Cat') THEN CAST(NULL AS int) WHEN ([Extent1].[Discriminator] = N'GoldFish') THEN CAST(NULL AS int) WHEN ([Extent1].[Discriminator] = N'Dog') THEN CAST(NULL AS int) WHEN ([Extent1].[Discriminator] = N'Parrot') THEN CAST(NULL AS int) WHEN ([Extent1].[Discriminator] = N'Turtle') THEN [Extent1].[NumberOfYears3] END AS [C12], 
    CASE WHEN ([Extent1].[Discriminator] = N'Cat') THEN CAST(NULL AS float) WHEN ([Extent1].[Discriminator] = N'GoldFish') THEN CAST(NULL AS float) WHEN ([Extent1].[Discriminator] = N'Dog') THEN CAST(NULL AS float) WHEN ([Extent1].[Discriminator] = N'Parrot') THEN CAST(NULL AS float) WHEN ([Extent1].[Discriminator] = N'Turtle') THEN [Extent1].[PercentStart1] END AS [C13], 
    CASE WHEN ([Extent1].[Discriminator] = N'Cat') THEN CAST(NULL AS float) WHEN ([Extent1].[Discriminator] = N'GoldFish') THEN CAST(NULL AS float) WHEN ([Extent1].[Discriminator] = N'Dog') THEN CAST(NULL AS float) WHEN ([Extent1].[Discriminator] = N'Parrot') THEN CAST(NULL AS float) WHEN ([Extent1].[Discriminator] = N'Turtle') THEN CAST(NULL AS float) ELSE [Extent1].[PercentOfBasis] END AS [C14], 
    CASE WHEN ([Extent1].[Discriminator] = N'Cat') THEN CAST(NULL AS varchar(1)) WHEN ([Extent1].[Discriminator] = N'GoldFish') THEN CAST(NULL AS varchar(1)) WHEN ([Extent1].[Discriminator] = N'Dog') THEN CAST(NULL AS varchar(1)) WHEN ([Extent1].[Discriminator] = N'Parrot') THEN CAST(NULL AS varchar(1)) WHEN ([Extent1].[Discriminator] = N'Turtle') THEN CAST(NULL AS varchar(1)) ELSE [Extent1].[ProductionBasisCSString] END AS [C15], 
    [Extent1].[Owner_Pet1_AnimalFK] AS [Owner_Pet1_AnimalFK], 
    [Extent1].[Owner_Pet2_AnimalFK] AS [Owner_Pet2_AnimalFK], 
    CASE WHEN ([Extent1].[Discriminator] = N'Cat') THEN CAST(NULL AS int) WHEN ([Extent1].[Discriminator] = N'GoldFish') THEN CAST(NULL AS int) WHEN ([Extent1].[Discriminator] = N'Dog') THEN [Extent1].[ExternalProfileInformation_ID] WHEN ([Extent1].[Discriminator] = N'Parrot') THEN CAST(NULL AS int) WHEN ([Extent1].[Discriminator] = N'Turtle') THEN CAST(NULL AS int) END AS [C16]
    FROM [dbo].[EFDepreciationRules] AS [Extent1]
    WHERE ([Extent1].[Discriminator] IN (N'Cat',N'GoldFish',N'Dog',N'Parrot',N'Turtle',N'EFDepRuleUnitProduction')) AND ((([Extent1].[Owner_Pet1_AnimalFK] IN (105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254)) AND ([Extent1].[Owner_Pet1_AnimalFK] IS NOT NULL)) OR (([Extent1].[Owner_Pet2_AnimalFK] IN (105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254)) AND ([Extent1].[Owner_Pet2_AnimalFK] IS NOT NULL)))
go

我的问题是如何解决这个问题?我试图建立一个TPC层次结构,但是根据我的理解,这对于这种情况不起作用。

  • 我应该重新定义我的LINQ查询吗?
  • TPC是否适合我的情况?
  • 我应该如何处理这种情况?

0 个答案:

没有答案