Reset all primary keys in the TPT hierarchy

Fixes #112
This commit is contained in:
Shay Rojansky
2021-11-19 13:55:23 +01:00
parent cb9130ed08
commit 0cd5825bcd
2 changed files with 59 additions and 7 deletions

View File

@@ -153,11 +153,50 @@ namespace EFCore.NamingConventions.Test
Assert.Equal("child_property", childEntityType.FindProperty(nameof(Child.ChildProperty))
.GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value));
var parentKey = parentEntityType.FindPrimaryKey();
var childKey = childEntityType.FindPrimaryKey();
var primaryKey = parentEntityType.FindPrimaryKey();
Assert.Same(primaryKey, childEntityType.FindPrimaryKey());
Assert.Equal("PK_parent", parentKey.GetName());
Assert.Equal("PK_parent", childKey.GetName());
Assert.Equal("PK_parent", primaryKey.GetName());
// For the following, see #112
var parentStoreObjectIdentifier = StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table).Value;
var childStoreObjectIdentifier = StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table).Value;
Assert.Equal("PK_parent", primaryKey.GetName(parentStoreObjectIdentifier));
Assert.Equal("PK_child", primaryKey.GetName(childStoreObjectIdentifier));
}
[Fact]
public void TPT_reversed_configuration()
{
var model = BuildModel(b =>
{
b.Entity<Child>().ToTable("child");
b.Entity<Parent>().ToTable("parent");
});
var parentEntityType = model.FindEntityType(typeof(Parent));
var childEntityType = model.FindEntityType(typeof(Child));
Assert.Equal("parent", parentEntityType.GetTableName());
Assert.Equal("id", parentEntityType.FindProperty(nameof(Parent.Id))
.GetColumnName(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table)!.Value));
Assert.Equal("parent_property", parentEntityType.FindProperty(nameof(Parent.ParentProperty))
.GetColumnName(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table)!.Value));
Assert.Equal("child", childEntityType.GetTableName());
Assert.Equal("child_property", childEntityType.FindProperty(nameof(Child.ChildProperty))
.GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value));
var primaryKey = parentEntityType.FindPrimaryKey();
Assert.Same(primaryKey, childEntityType.FindPrimaryKey());
Assert.Equal("PK_parent", primaryKey.GetName());
// For the following, see #112
var parentStoreObjectIdentifier = StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table).Value;
var childStoreObjectIdentifier = StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table).Value;
Assert.Equal("PK_parent", primaryKey.GetName(parentStoreObjectIdentifier));
Assert.Equal("PK_child", primaryKey.GetName(childStoreObjectIdentifier));
}
[Fact]

View File

@@ -136,14 +136,27 @@ namespace EFCore.NamingConventions.Internal
// However, this isn't yet supported with TPT, see https://github.com/dotnet/efcore/issues/23444.
// So we need to check if the entity is within a TPT hierarchy, or is an owned entity within a TPT hierarchy.
if (entityType.FindRowInternalForeignKeys(tableIdentifier).FirstOrDefault() is null
&& (entityType.BaseType is null || entityType.GetTableName() == entityType.BaseType.GetTableName()))
var rootType = entityType.GetRootType();
var isTPT = rootType.GetDerivedTypes().FirstOrDefault() is { } derivedType
&& derivedType.GetTableName() != rootType.GetTableName();
if (entityType.FindRowInternalForeignKeys(tableIdentifier).FirstOrDefault() is null && !isTPT)
{
primaryKey.Builder.HasName(_namingNameRewriter.RewriteName(primaryKey.GetDefaultName()));
}
else
{
primaryKey.Builder.HasNoAnnotation(RelationalAnnotationNames.Name);
// This hierarchy is being transformed into TPT via the explicit setting of the table name.
// We not only have to reset our own key name, but also the parents'. Otherwise, the parent's key name
// is used as the child's (see RelationalKeyExtensions.GetName), and we get a "duplicate key name in database" error
// since both parent and child have the same key name;
foreach (var type in entityType.GetRootType().GetDerivedTypesInclusive())
{
if (type.FindPrimaryKey() is IConventionKey pk)
{
pk.Builder.HasNoAnnotation(RelationalAnnotationNames.Name);
}
}
}
}