Skip to content

Commit

Permalink
Don't set table names from DbSet properties when Discriminator proper…
Browse files Browse the repository at this point in the history
…ty is conflicted with the default but is then configured explicitly (#30234)
  • Loading branch information
ajcvickers authored Feb 11, 2023
1 parent a959f2e commit 8af9ad1
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ public virtual void ProcessEntityTypeAnnotationChanged(
&& (entityTypeBuilder.Metadata.GetMappingStrategy() ?? RelationalAnnotationNames.TphMappingStrategy)
!= RelationalAnnotationNames.TphMappingStrategy)
{
foreach (var deriverEntityType in entityTypeBuilder.Metadata.GetDerivedTypesInclusive())
foreach (var derivedEntityType in entityTypeBuilder.Metadata.GetDerivedTypesInclusive())
{
if (!deriverEntityType.HasSharedClrType
&& _sets.TryGetValue(deriverEntityType.ClrType, out var setName))
if (!derivedEntityType.HasSharedClrType
&& _sets.TryGetValue(derivedEntityType.ClrType, out var setName))
{
deriverEntityType.Builder.ToTable(setName);
derivedEntityType.Builder.ToTable(setName);
}
}
}
Expand All @@ -145,12 +145,21 @@ public virtual void ProcessModelFinalizing(
entityType.Builder.HasNoAnnotation(RelationalAnnotationNames.TableName);
}

if (entityType.GetMappingStrategy() == RelationalAnnotationNames.TpcMappingStrategy
var mappingStrategy = entityType.GetMappingStrategy();

if (mappingStrategy == RelationalAnnotationNames.TpcMappingStrategy
&& entityType.IsAbstract())
{
// Undo the convention change if the entity type is mapped using TPC
entityType.Builder.HasNoAnnotation(RelationalAnnotationNames.TableName);
}

if (mappingStrategy == RelationalAnnotationNames.TphMappingStrategy
&& entityType.BaseType != null)
{
// Undo the convention change if the hierarchy ultimately ends up TPH
entityType.Builder.HasNoAnnotation(RelationalAnnotationNames.TableName);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ public override async Task Can_query_when_shared_column(bool async)
"""
SELECT c
FROM root c
WHERE (c["Discriminator"] = "Coke")
WHERE (c["Discriminator"] = 1)
OFFSET 0 LIMIT 2
""",
//
"""
SELECT c
FROM root c
WHERE (c["Discriminator"] = "Lilt")
WHERE (c["Discriminator"] = 2)
OFFSET 0 LIMIT 2
""",
//
"""
SELECT c
FROM root c
WHERE (c["Discriminator"] = "Tea")
WHERE (c["Discriminator"] = 3)
OFFSET 0 LIMIT 2
""");
}
Expand All @@ -47,7 +47,7 @@ public override async Task Can_query_all_types_when_shared_column(bool async)
"""
SELECT c
FROM root c
WHERE c["Discriminator"] IN ("Drink", "Coke", "Lilt", "Tea")
WHERE c["Discriminator"] IN (0, 1, 2, 3)
""");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,18 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
if (HasDiscriminator)
{
modelBuilder.Entity<Bird>().HasDiscriminator<string>("Discriminator").IsComplete(IsDiscriminatorMappingComplete);
modelBuilder.Entity<Drink>().HasDiscriminator().IsComplete(IsDiscriminatorMappingComplete);

modelBuilder.Entity<Drink>()
.HasDiscriminator(e => e.Discriminator)
.HasValue<Drink>(DrinkType.Drink)
.HasValue<Coke>(DrinkType.Coke)
.HasValue<Lilt>(DrinkType.Lilt)
.HasValue<Tea>(DrinkType.Tea)
.IsComplete(IsDiscriminatorMappingComplete);
}
else
{
modelBuilder.Entity<Drink>().Ignore(e => e.Discriminator);
}

modelBuilder.Entity<KiwiQuery>().HasDiscriminator().IsComplete(IsDiscriminatorMappingComplete);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public class Drink
{
public int Id { get; set; }
public int SortIndex { get; set; }
public DrinkType Discriminator { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.TestModels.InheritanceModel;

public enum DrinkType
{
Drink,
Coke,
Lilt,
Tea
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public InheritanceContext(DbContextOptions options)
public DbSet<AnimalQuery> AnimalQueries { get; set; }
public DbSet<Country> Countries { get; set; }
public DbSet<Drink> Drinks { get; set; }
public DbSet<Coke> Coke { get; set; }
public DbSet<Lilt> Lilt { get; set; }
public DbSet<Tea> Tea { get; set; }
public DbSet<Plant> Plants { get; set; }

public static void Seed(InheritanceContext context, bool useGeneratedKeys)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public override async Task Update_with_interface_in_property_expression(bool asy
UPDATE [d]
SET [d].[SugarGrams] = 0
FROM [Drinks] AS [d]
WHERE [d].[Discriminator] = N'Coke'
WHERE [d].[Discriminator] = 1
""");
}

Expand All @@ -231,7 +231,7 @@ public override async Task Update_with_interface_in_EF_Property_in_property_expr
UPDATE [d]
SET [d].[SugarGrams] = 0
FROM [Drinks] AS [d]
WHERE [d].[Discriminator] = N'Coke'
WHERE [d].[Discriminator] = 1
""");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ public override async Task Can_query_when_shared_column(bool async)
"""
SELECT TOP(2) [d].[Id], [d].[Discriminator], [d].[SortIndex], [d].[CaffeineGrams], [d].[CokeCO2], [d].[SugarGrams]
FROM [Drinks] AS [d]
WHERE [d].[Discriminator] = N'Coke'
WHERE [d].[Discriminator] = 1
""",
//
"""
SELECT TOP(2) [d].[Id], [d].[Discriminator], [d].[SortIndex], [d].[LiltCO2], [d].[SugarGrams]
FROM [Drinks] AS [d]
WHERE [d].[Discriminator] = N'Lilt'
WHERE [d].[Discriminator] = 2
""",
//
"""
SELECT TOP(2) [d].[Id], [d].[Discriminator], [d].[SortIndex], [d].[CaffeineGrams], [d].[HasMilk]
FROM [Drinks] AS [d]
WHERE [d].[Discriminator] = N'Tea'
WHERE [d].[Discriminator] = 3
""");
}

Expand Down Expand Up @@ -103,7 +103,7 @@ public override async Task Can_query_all_types_when_shared_column(bool async)
"""
SELECT [d].[Id], [d].[Discriminator], [d].[SortIndex], [d].[CaffeineGrams], [d].[CokeCO2], [d].[SugarGrams], [d].[LiltCO2], [d].[HasMilk]
FROM [Drinks] AS [d]
WHERE [d].[Discriminator] IN (N'Drink', N'Coke', N'Lilt', N'Tea')
WHERE [d].[Discriminator] IN (0, 1, 2, 3)
""");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ public override async Task Can_query_when_shared_column(bool async)
"""
SELECT TOP(2) [d].[Id], [d].[Discriminator], [d].[SortIndex], [d].[CaffeineGrams], [d].[CokeCO2], [d].[SugarGrams]
FROM [Drinks] AS [d]
WHERE [d].[Discriminator] = N'Coke'
WHERE [d].[Discriminator] = 1
""",
//
"""
SELECT TOP(2) [d].[Id], [d].[Discriminator], [d].[SortIndex], [d].[LiltCO2], [d].[SugarGrams]
FROM [Drinks] AS [d]
WHERE [d].[Discriminator] = N'Lilt'
WHERE [d].[Discriminator] = 2
""",
//
"""
SELECT TOP(2) [d].[Id], [d].[Discriminator], [d].[SortIndex], [d].[CaffeineGrams], [d].[HasMilk]
FROM [Drinks] AS [d]
WHERE [d].[Discriminator] = N'Tea'
WHERE [d].[Discriminator] = 3
""");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public override async Task Update_with_interface_in_property_expression(bool asy
"""
UPDATE "Drinks" AS "d"
SET "SugarGrams" = 0
WHERE "d"."Discriminator" = 'Coke'
WHERE "d"."Discriminator" = 1
""");
}

Expand All @@ -219,7 +219,7 @@ public override async Task Update_with_interface_in_EF_Property_in_property_expr
"""
UPDATE "Drinks" AS "d"
SET "SugarGrams" = 0
WHERE "d"."Discriminator" = 'Coke'
WHERE "d"."Discriminator" = 1
""");
}

Expand Down

0 comments on commit 8af9ad1

Please sign in to comment.