TPC support (#182)

Also turn on nullability for the tests

Closes #181
Closes #177
This commit is contained in:
Shay Rojansky
2023-01-13 18:17:43 +01:00
committed by GitHub
parent 81d512004d
commit 8ce1fb1bb0
5 changed files with 178 additions and 93 deletions

View File

@@ -1,5 +1,7 @@
<Project> <Project>
<PropertyGroup> <PropertyGroup>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<AnalysisLevel>latest</AnalysisLevel> <AnalysisLevel>latest</AnalysisLevel>
<IncludeSymbols>true</IncludeSymbols> <IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat> <SymbolPackageFormat>snupkg</SymbolPackageFormat>

View File

@@ -2,6 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@@ -26,7 +26,7 @@ public class NameRewritingConventionTest
public void Column() public void Column()
{ {
var entityType = BuildEntityType(b => b.Entity<SampleEntity>()); var entityType = BuildEntityType(b => b.Entity<SampleEntity>());
Assert.Equal("sample_entity_id", entityType.FindProperty(nameof(SampleEntity.SampleEntityId)) Assert.Equal("sample_entity_id", entityType.FindProperty(nameof(SampleEntity.SampleEntityId))!
.GetColumnName(StoreObjectIdentifier.Create(entityType, StoreObjectType.Table)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(entityType, StoreObjectType.Table)!.Value));
} }
@@ -36,7 +36,7 @@ public class NameRewritingConventionTest
var entityType = BuildEntityType( var entityType = BuildEntityType(
b => b.Entity<SampleEntity>(), b => b.Entity<SampleEntity>(),
CultureInfo.CreateSpecificCulture("tr-TR")); CultureInfo.CreateSpecificCulture("tr-TR"));
Assert.Equal("sample_entity_ıd", entityType.FindProperty(nameof(SampleEntity.SampleEntityId)) Assert.Equal("sample_entity_ıd", entityType.FindProperty(nameof(SampleEntity.SampleEntityId))!
.GetColumnName(StoreObjectIdentifier.Create(entityType, StoreObjectType.Table)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(entityType, StoreObjectType.Table)!.Value));
} }
@@ -46,7 +46,7 @@ public class NameRewritingConventionTest
var entityType = BuildEntityType( var entityType = BuildEntityType(
b => b.Entity<SampleEntity>(), b => b.Entity<SampleEntity>(),
CultureInfo.InvariantCulture); CultureInfo.InvariantCulture);
Assert.Equal("sample_entity_id", entityType.FindProperty(nameof(SampleEntity.SampleEntityId)) Assert.Equal("sample_entity_id", entityType.FindProperty(nameof(SampleEntity.SampleEntityId))!
.GetColumnName(StoreObjectIdentifier.Create(entityType, StoreObjectType.Table)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(entityType, StoreObjectType.Table)!.Value));
} }
@@ -63,7 +63,7 @@ public class NameRewritingConventionTest
foreach (var type in new[] { StoreObjectType.Table, StoreObjectType.View, StoreObjectType.Function }) foreach (var type in new[] { StoreObjectType.Table, StoreObjectType.View, StoreObjectType.Function })
{ {
Assert.Equal("sample_entity_id", entityType.FindProperty(nameof(SampleEntity.SampleEntityId)) Assert.Equal("sample_entity_id", entityType.FindProperty(nameof(SampleEntity.SampleEntityId))!
.GetColumnName(StoreObjectIdentifier.Create(entityType, type)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(entityType, type)!.Value));
} }
} }
@@ -100,7 +100,7 @@ public class NameRewritingConventionTest
public void Foreign_key() public void Foreign_key()
{ {
var model = BuildModel(b => b.Entity<Blog>()); var model = BuildModel(b => b.Entity<Blog>());
var entityType = model.FindEntityType(typeof(Post)); var entityType = model.FindEntityType(typeof(Post))!;
Assert.Equal("fk_post_blog_blog_id", entityType.GetForeignKeys().Single().GetConstraintName()); Assert.Equal("fk_post_blog_blog_id", entityType.GetForeignKeys().Single().GetConstraintName());
} }
@@ -120,17 +120,17 @@ public class NameRewritingConventionTest
b.Entity<Child>(); b.Entity<Child>();
}); });
var parentEntityType = model.FindEntityType(typeof(Parent)); var parentEntityType = model.FindEntityType(typeof(Parent))!;
var childEntityType = model.FindEntityType(typeof(Child)); var childEntityType = model.FindEntityType(typeof(Child))!;
Assert.Equal("parent", parentEntityType.GetTableName()); Assert.Equal("parent", parentEntityType.GetTableName());
Assert.Equal("id", parentEntityType.FindProperty(nameof(Parent.Id)) Assert.Equal("id", parentEntityType.FindProperty(nameof(Parent.Id))!
.GetColumnName(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table)!.Value));
Assert.Equal("parent_property", parentEntityType.FindProperty(nameof(Parent.ParentProperty)) Assert.Equal("parent_property", parentEntityType.FindProperty(nameof(Parent.ParentProperty))!
.GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value));
Assert.Equal("parent", childEntityType.GetTableName()); Assert.Equal("parent", childEntityType.GetTableName());
Assert.Equal("child_property", childEntityType.FindProperty(nameof(Child.ChildProperty)) Assert.Equal("child_property", childEntityType.FindProperty(nameof(Child.ChildProperty))!
.GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value));
Assert.Same(parentEntityType.FindPrimaryKey(), childEntityType.FindPrimaryKey()); Assert.Same(parentEntityType.FindPrimaryKey(), childEntityType.FindPrimaryKey());
@@ -145,27 +145,27 @@ public class NameRewritingConventionTest
b.Entity<Child>().ToTable("child"); b.Entity<Child>().ToTable("child");
}); });
var parentEntityType = model.FindEntityType(typeof(Parent)); var parentEntityType = model.FindEntityType(typeof(Parent))!;
var childEntityType = model.FindEntityType(typeof(Child)); var childEntityType = model.FindEntityType(typeof(Child))!;
Assert.Equal("parent", parentEntityType.GetTableName()); Assert.Equal("parent", parentEntityType.GetTableName());
Assert.Equal("id", parentEntityType.FindProperty(nameof(Parent.Id)) Assert.Equal("id", parentEntityType.FindProperty(nameof(Parent.Id))!
.GetColumnName(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table)!.Value));
Assert.Equal("parent_property", parentEntityType.FindProperty(nameof(Parent.ParentProperty)) Assert.Equal("parent_property", parentEntityType.FindProperty(nameof(Parent.ParentProperty))!
.GetColumnName(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table)!.Value));
Assert.Equal("child", childEntityType.GetTableName()); Assert.Equal("child", childEntityType.GetTableName());
Assert.Equal("child_property", childEntityType.FindProperty(nameof(Child.ChildProperty)) Assert.Equal("child_property", childEntityType.FindProperty(nameof(Child.ChildProperty))!
.GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value));
var primaryKey = parentEntityType.FindPrimaryKey(); var primaryKey = parentEntityType.FindPrimaryKey()!;
Assert.Same(primaryKey, childEntityType.FindPrimaryKey()); Assert.Same(primaryKey, childEntityType.FindPrimaryKey());
Assert.Equal("PK_parent", primaryKey.GetName()); Assert.Equal("PK_parent", primaryKey.GetName());
// For the following, see #112 // For the following, see #112
var parentStoreObjectIdentifier = StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table).Value; var parentStoreObjectIdentifier = StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table)!.Value;
var childStoreObjectIdentifier = StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table).Value; var childStoreObjectIdentifier = StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value;
Assert.Equal("PK_parent", primaryKey.GetName(parentStoreObjectIdentifier)); Assert.Equal("PK_parent", primaryKey.GetName(parentStoreObjectIdentifier));
Assert.Equal("PK_child", primaryKey.GetName(childStoreObjectIdentifier)); Assert.Equal("PK_child", primaryKey.GetName(childStoreObjectIdentifier));
} }
@@ -179,27 +179,27 @@ public class NameRewritingConventionTest
b.Entity<Parent>().ToTable("parent"); b.Entity<Parent>().ToTable("parent");
}); });
var parentEntityType = model.FindEntityType(typeof(Parent)); var parentEntityType = model.FindEntityType(typeof(Parent))!;
var childEntityType = model.FindEntityType(typeof(Child)); var childEntityType = model.FindEntityType(typeof(Child))!;
Assert.Equal("parent", parentEntityType.GetTableName()); Assert.Equal("parent", parentEntityType.GetTableName());
Assert.Equal("id", parentEntityType.FindProperty(nameof(Parent.Id)) Assert.Equal("id", parentEntityType.FindProperty(nameof(Parent.Id))!
.GetColumnName(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table)!.Value));
Assert.Equal("parent_property", parentEntityType.FindProperty(nameof(Parent.ParentProperty)) Assert.Equal("parent_property", parentEntityType.FindProperty(nameof(Parent.ParentProperty))!
.GetColumnName(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table)!.Value));
Assert.Equal("child", childEntityType.GetTableName()); Assert.Equal("child", childEntityType.GetTableName());
Assert.Equal("child_property", childEntityType.FindProperty(nameof(Child.ChildProperty)) Assert.Equal("child_property", childEntityType.FindProperty(nameof(Child.ChildProperty))!
.GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value));
var primaryKey = parentEntityType.FindPrimaryKey(); var primaryKey = parentEntityType.FindPrimaryKey()!;
Assert.Same(primaryKey, childEntityType.FindPrimaryKey()); Assert.Same(primaryKey, childEntityType.FindPrimaryKey());
Assert.Equal("PK_parent", primaryKey.GetName()); Assert.Equal("PK_parent", primaryKey.GetName());
// For the following, see #112 // For the following, see #112
var parentStoreObjectIdentifier = StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table).Value; var parentStoreObjectIdentifier = StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table)!.Value;
var childStoreObjectIdentifier = StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table).Value; var childStoreObjectIdentifier = StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value;
Assert.Equal("PK_parent", primaryKey.GetName(parentStoreObjectIdentifier)); Assert.Equal("PK_parent", primaryKey.GetName(parentStoreObjectIdentifier));
Assert.Equal("PK_child", primaryKey.GetName(childStoreObjectIdentifier)); Assert.Equal("PK_child", primaryKey.GetName(childStoreObjectIdentifier));
} }
@@ -213,24 +213,24 @@ public class NameRewritingConventionTest
b.Entity<ChildWithOwned>().OwnsOne(c => c.Owned); b.Entity<ChildWithOwned>().OwnsOne(c => c.Owned);
}); });
var parentEntityType = model.FindEntityType(typeof(Parent)); var parentEntityType = model.FindEntityType(typeof(Parent))!;
var childEntityType = model.FindEntityType(typeof(ChildWithOwned)); var childEntityType = model.FindEntityType(typeof(ChildWithOwned))!;
var ownedEntityType = model.FindEntityType(typeof(Owned)); var ownedEntityType = model.FindEntityType(typeof(Owned))!;
Assert.Equal("parent", parentEntityType.GetTableName()); Assert.Equal("parent", parentEntityType.GetTableName());
Assert.Equal("id", parentEntityType.FindProperty(nameof(Parent.Id)) Assert.Equal("id", parentEntityType.FindProperty(nameof(Parent.Id))!
.GetColumnName(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table)!.Value));
Assert.Equal("parent_property", parentEntityType.FindProperty(nameof(Parent.ParentProperty)) Assert.Equal("parent_property", parentEntityType.FindProperty(nameof(Parent.ParentProperty))!
.GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value));
Assert.Equal("parent", childEntityType.GetTableName()); Assert.Equal("parent", childEntityType.GetTableName());
Assert.Equal("child_property", childEntityType.FindProperty(nameof(Child.ChildProperty)) Assert.Equal("child_property", childEntityType.FindProperty(nameof(Child.ChildProperty))!
.GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value));
Assert.Same(parentEntityType.FindPrimaryKey(), childEntityType.FindPrimaryKey()); Assert.Same(parentEntityType.FindPrimaryKey(), childEntityType.FindPrimaryKey());
Assert.Equal("parent", ownedEntityType.GetTableName()); Assert.Equal("parent", ownedEntityType.GetTableName());
Assert.Equal("owned_owned_property", ownedEntityType.FindProperty(nameof(Owned.OwnedProperty)) Assert.Equal("owned_owned_property", ownedEntityType.FindProperty(nameof(Owned.OwnedProperty))!
.GetColumnName(StoreObjectIdentifier.Create(ownedEntityType, StoreObjectType.Table)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(ownedEntityType, StoreObjectType.Table)!.Value));
} }
@@ -248,31 +248,102 @@ public class NameRewritingConventionTest
}); });
}); });
var parentEntityType = model.FindEntityType(typeof(Parent)); var parentEntityType = model.FindEntityType(typeof(Parent))!;
var childEntityType = model.FindEntityType(typeof(ChildWithOwned)); var childEntityType = model.FindEntityType(typeof(ChildWithOwned))!;
var ownedEntityType = model.FindEntityType(typeof(Owned)); var ownedEntityType = model.FindEntityType(typeof(Owned))!;
Assert.Equal("parent", parentEntityType.GetTableName()); Assert.Equal("parent", parentEntityType.GetTableName());
Assert.Equal("id", parentEntityType.FindProperty(nameof(Parent.Id)) Assert.Equal("id", parentEntityType.FindProperty(nameof(Parent.Id))!
.GetColumnName(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table)!.Value));
Assert.Equal("parent_property", parentEntityType.FindProperty(nameof(Parent.ParentProperty)) Assert.Equal("parent_property", parentEntityType.FindProperty(nameof(Parent.ParentProperty))!
.GetColumnName(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table)!.Value));
Assert.Equal("child", childEntityType.GetTableName()); Assert.Equal("child", childEntityType.GetTableName());
Assert.Equal("child_property", childEntityType.FindProperty(nameof(ChildWithOwned.ChildProperty)) Assert.Equal("child_property", childEntityType.FindProperty(nameof(ChildWithOwned.ChildProperty))!
.GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value));
var parentKey = parentEntityType.FindPrimaryKey(); var parentKey = parentEntityType.FindPrimaryKey()!;
var childKey = childEntityType.FindPrimaryKey(); var childKey = childEntityType.FindPrimaryKey()!;
Assert.Equal("PK_parent", parentKey.GetName()); Assert.Equal("PK_parent", parentKey.GetName());
Assert.Equal("PK_parent", childKey.GetName()); Assert.Equal("PK_parent", childKey.GetName());
Assert.Equal("child", ownedEntityType.GetTableName()); Assert.Equal("child", ownedEntityType.GetTableName());
Assert.Equal("owned_owned_property", ownedEntityType.FindProperty(nameof(Owned.OwnedProperty)) Assert.Equal("owned_owned_property", ownedEntityType.FindProperty(nameof(Owned.OwnedProperty))!
.GetColumnName(StoreObjectIdentifier.Create(ownedEntityType, StoreObjectType.Table)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(ownedEntityType, StoreObjectType.Table)!.Value));
} }
[Fact]
public void TPC()
{
var model = BuildModel(b =>
{
b.Entity<Parent>().UseTpcMappingStrategy();
b.Entity<Child>();
});
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("id", childEntityType.FindProperty(nameof(Parent.Id))!
.GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value));
Assert.Equal("parent_property", childEntityType.FindProperty(nameof(Parent.ParentProperty))!
.GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value));
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]
public void TPC_with_abstract_parent()
{
var model = BuildModel(b =>
{
b.Entity<AbstractParent>().UseTpcMappingStrategy();
b.Entity<ChildOfAbstract>();
});
var parentEntityType = model.FindEntityType(typeof(AbstractParent))!;
var childEntityType = model.FindEntityType(typeof(ChildOfAbstract))!;
Assert.Null(parentEntityType.GetTableName());
Assert.Null(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table));
Assert.Equal("child_of_abstract", childEntityType.GetTableName());
Assert.Equal("id", childEntityType.FindProperty(nameof(AbstractParent.Id))!
.GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value));
Assert.Equal("parent_property", childEntityType.FindProperty(nameof(AbstractParent.ParentProperty))!
.GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value));
Assert.Equal("child_property", childEntityType.FindProperty(nameof(ChildOfAbstract.ChildProperty))!
.GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value));
var primaryKey = parentEntityType.FindPrimaryKey()!;
Assert.Same(primaryKey, childEntityType.FindPrimaryKey());
Assert.Null(primaryKey.GetName());
// For the following, see #112
var childStoreObjectIdentifier = StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table)!.Value;
Assert.Equal("PK_child_of_abstract", primaryKey.GetName(childStoreObjectIdentifier));
}
[Fact] [Fact]
public void Table_splitting1() public void Table_splitting1()
{ {
@@ -288,25 +359,25 @@ public class NameRewritingConventionTest
b.Entity<Split2>().ToTable("split_table"); b.Entity<Split2>().ToTable("split_table");
}); });
var split1EntityType = model.FindEntityType(typeof(Split1)); var split1EntityType = model.FindEntityType(typeof(Split1))!;
var split2EntityType = model.FindEntityType(typeof(Split2)); var split2EntityType = model.FindEntityType(typeof(Split2))!;
var table = StoreObjectIdentifier.Create(split1EntityType, StoreObjectType.Table)!.Value; var table = StoreObjectIdentifier.Create(split1EntityType, StoreObjectType.Table)!.Value;
Assert.Equal(table, StoreObjectIdentifier.Create(split2EntityType, StoreObjectType.Table)); Assert.Equal(table, StoreObjectIdentifier.Create(split2EntityType, StoreObjectType.Table));
Assert.Equal("split_table", split1EntityType.GetTableName()); Assert.Equal("split_table", split1EntityType.GetTableName());
Assert.Equal("one_prop", split1EntityType.FindProperty(nameof(Split1.OneProp)).GetColumnName(table)); Assert.Equal("one_prop", split1EntityType.FindProperty(nameof(Split1.OneProp))!.GetColumnName(table));
Assert.Equal("split_table", split2EntityType.GetTableName()); Assert.Equal("split_table", split2EntityType.GetTableName());
Assert.Equal("two_prop", split2EntityType.FindProperty(nameof(Split2.TwoProp)).GetColumnName(table)); Assert.Equal("two_prop", split2EntityType.FindProperty(nameof(Split2.TwoProp))!.GetColumnName(table));
Assert.Equal("common", split1EntityType.FindProperty(nameof(Split1.Common)).GetColumnName(table)); Assert.Equal("common", split1EntityType.FindProperty(nameof(Split1.Common))!.GetColumnName(table));
Assert.Equal("split2_common", split2EntityType.FindProperty(nameof(Split2.Common)).GetColumnName(table)); Assert.Equal("split2_common", split2EntityType.FindProperty(nameof(Split2.Common))!.GetColumnName(table));
var foreignKey = split2EntityType.GetForeignKeys().Single(); var foreignKey = split2EntityType.GetForeignKeys().Single();
Assert.Same(split1EntityType.FindPrimaryKey(), foreignKey.PrincipalKey); Assert.Same(split1EntityType.FindPrimaryKey(), foreignKey.PrincipalKey);
Assert.Same(split2EntityType.FindPrimaryKey().Properties.Single(), foreignKey.Properties.Single()); Assert.Same(split2EntityType.FindPrimaryKey()!.Properties.Single(), foreignKey.Properties.Single());
Assert.Equal(split1EntityType.FindPrimaryKey().GetName(), split2EntityType.FindPrimaryKey().GetName()); Assert.Equal(split1EntityType.FindPrimaryKey()!.GetName(), split2EntityType.FindPrimaryKey()!.GetName());
Assert.Equal( Assert.Equal(
foreignKey.PrincipalKey.Properties.Single().GetColumnName(table), foreignKey.PrincipalKey.Properties.Single().GetColumnName(table),
foreignKey.Properties.Single().GetColumnName(table)); foreignKey.Properties.Single().GetColumnName(table));
@@ -323,25 +394,25 @@ public class NameRewritingConventionTest
b.Entity<Split1>().ToTable("split_table"); b.Entity<Split1>().ToTable("split_table");
}); });
var split1EntityType = model.FindEntityType(typeof(Split1)); var split1EntityType = model.FindEntityType(typeof(Split1))!;
var split2EntityType = model.FindEntityType(typeof(Split2)); var split2EntityType = model.FindEntityType(typeof(Split2))!;
var table = StoreObjectIdentifier.Create(split1EntityType, StoreObjectType.Table)!.Value; var table = StoreObjectIdentifier.Create(split1EntityType, StoreObjectType.Table)!.Value;
Assert.Equal(table, StoreObjectIdentifier.Create(split2EntityType, StoreObjectType.Table)); Assert.Equal(table, StoreObjectIdentifier.Create(split2EntityType, StoreObjectType.Table));
Assert.Equal("split_table", split1EntityType.GetTableName()); Assert.Equal("split_table", split1EntityType.GetTableName());
Assert.Equal("one_prop", split1EntityType.FindProperty(nameof(Split1.OneProp)).GetColumnName(table)); Assert.Equal("one_prop", split1EntityType.FindProperty(nameof(Split1.OneProp))!.GetColumnName(table));
Assert.Equal("split_table", split2EntityType.GetTableName()); Assert.Equal("split_table", split2EntityType.GetTableName());
Assert.Equal("two_prop", split2EntityType.FindProperty(nameof(Split2.TwoProp)).GetColumnName(table)); Assert.Equal("two_prop", split2EntityType.FindProperty(nameof(Split2.TwoProp))!.GetColumnName(table));
Assert.Equal("common", split1EntityType.FindProperty(nameof(Split1.Common)).GetColumnName(table)); Assert.Equal("common", split1EntityType.FindProperty(nameof(Split1.Common))!.GetColumnName(table));
Assert.Equal("split2_common", split2EntityType.FindProperty(nameof(Split2.Common)).GetColumnName(table)); Assert.Equal("split2_common", split2EntityType.FindProperty(nameof(Split2.Common))!.GetColumnName(table));
var foreignKey = split2EntityType.GetForeignKeys().Single(); var foreignKey = split2EntityType.GetForeignKeys().Single();
Assert.Same(split1EntityType.FindPrimaryKey(), foreignKey.PrincipalKey); Assert.Same(split1EntityType.FindPrimaryKey(), foreignKey.PrincipalKey);
Assert.Same(split2EntityType.FindPrimaryKey().Properties.Single(), foreignKey.Properties.Single()); Assert.Same(split2EntityType.FindPrimaryKey()!.Properties.Single(), foreignKey.Properties.Single());
Assert.Equal(split1EntityType.FindPrimaryKey().GetName(), split2EntityType.FindPrimaryKey().GetName()); Assert.Equal(split1EntityType.FindPrimaryKey()!.GetName(), split2EntityType.FindPrimaryKey()!.GetName());
Assert.Equal( Assert.Equal(
foreignKey.PrincipalKey.Properties.Single().GetColumnName(table), foreignKey.PrincipalKey.Properties.Single().GetColumnName(table),
foreignKey.Properties.Single().GetColumnName(table)); foreignKey.Properties.Single().GetColumnName(table));
@@ -353,17 +424,17 @@ public class NameRewritingConventionTest
{ {
var model = BuildModel(b => b.Entity<Owner>().OwnsOne(o => o.Owned)); var model = BuildModel(b => b.Entity<Owner>().OwnsOne(o => o.Owned));
var ownerEntityType = model.FindEntityType(typeof(Owner)); var ownerEntityType = model.FindEntityType(typeof(Owner))!;
var ownedEntityType = model.FindEntityType(typeof(Owned)); var ownedEntityType = model.FindEntityType(typeof(Owned))!;
Assert.Equal("owner", ownerEntityType.GetTableName()); Assert.Equal("owner", ownerEntityType.GetTableName());
Assert.Equal("owner", ownedEntityType.GetTableName()); Assert.Equal("owner", ownedEntityType.GetTableName());
var table = StoreObjectIdentifier.Create(ownerEntityType, StoreObjectType.Table)!.Value; var table = StoreObjectIdentifier.Create(ownerEntityType, StoreObjectType.Table)!.Value;
Assert.Equal(table, StoreObjectIdentifier.Create(ownedEntityType, StoreObjectType.Table)!.Value); Assert.Equal(table, StoreObjectIdentifier.Create(ownedEntityType, StoreObjectType.Table)!.Value);
Assert.Equal("owned_owned_property", ownedEntityType.FindProperty(nameof(Owned.OwnedProperty)).GetColumnName(table)); Assert.Equal("owned_owned_property", ownedEntityType.FindProperty(nameof(Owned.OwnedProperty))!.GetColumnName(table));
var (ownerKey, ownedKey) = (ownerEntityType.FindPrimaryKey(), ownedEntityType.FindPrimaryKey()); var (ownerKey, ownedKey) = (ownerEntityType.FindPrimaryKey()!, ownedEntityType.FindPrimaryKey()!);
Assert.Equal("pk_owner", ownerKey.GetName()); Assert.Equal("pk_owner", ownerKey.GetName());
Assert.Equal("pk_owner", ownedKey.GetName()); Assert.Equal("pk_owner", ownedKey.GetName());
Assert.Equal("id", ownerKey.Properties.Single().GetColumnName(table)); Assert.Equal("id", ownerKey.Properties.Single().GetColumnName(table));
@@ -381,17 +452,17 @@ public class NameRewritingConventionTest
e.ToTable("destination_table"); e.ToTable("destination_table");
})); }));
var ownerEntityType = model.FindEntityType(typeof(Owner)); var ownerEntityType = model.FindEntityType(typeof(Owner))!;
var ownedEntityType = model.FindEntityType(typeof(Owned)); var ownedEntityType = model.FindEntityType(typeof(Owned))!;
Assert.Equal("destination_table", ownerEntityType.GetTableName()); Assert.Equal("destination_table", ownerEntityType.GetTableName());
Assert.Equal("destination_table", ownedEntityType.GetTableName()); Assert.Equal("destination_table", ownedEntityType.GetTableName());
var table = StoreObjectIdentifier.Create(ownerEntityType, StoreObjectType.Table)!.Value; var table = StoreObjectIdentifier.Create(ownerEntityType, StoreObjectType.Table)!.Value;
Assert.Equal(table, StoreObjectIdentifier.Create(ownedEntityType, StoreObjectType.Table)!.Value); Assert.Equal(table, StoreObjectIdentifier.Create(ownedEntityType, StoreObjectType.Table)!.Value);
Assert.Equal("owned_owned_property", ownedEntityType.FindProperty(nameof(Owned.OwnedProperty)).GetColumnName(table)); Assert.Equal("owned_owned_property", ownedEntityType.FindProperty(nameof(Owned.OwnedProperty))!.GetColumnName(table));
var (ownerKey, ownedKey) = (ownerEntityType.FindPrimaryKey(), ownedEntityType.FindPrimaryKey()); var (ownerKey, ownedKey) = (ownerEntityType.FindPrimaryKey()!, ownedEntityType.FindPrimaryKey()!);
Assert.Equal("pk_destination_table", ownerKey.GetName()); Assert.Equal("pk_destination_table", ownerKey.GetName());
Assert.Equal("pk_destination_table", ownedKey.GetName()); Assert.Equal("pk_destination_table", ownedKey.GetName());
Assert.Equal("id", ownerKey.Properties.Single().GetColumnName(table)); Assert.Equal("id", ownerKey.Properties.Single().GetColumnName(table));
@@ -410,9 +481,9 @@ public class NameRewritingConventionTest
e.ToTable("destination_table"); e.ToTable("destination_table");
})); }));
var ownerEntityType = model.FindEntityType(typeof(Owner)); var ownerEntityType = model.FindEntityType(typeof(Owner))!;
var owned1EntityType = model.FindEntityType("owned1"); var owned1EntityType = model.FindEntityType("owned1")!;
var owned2EntityType = model.FindEntityType("owned2"); var owned2EntityType = model.FindEntityType("owned2")!;
Assert.Equal("destination_table", ownerEntityType.GetTableName()); Assert.Equal("destination_table", ownerEntityType.GetTableName());
Assert.Equal("destination_table", owned1EntityType.GetTableName()); Assert.Equal("destination_table", owned1EntityType.GetTableName());
@@ -421,11 +492,11 @@ public class NameRewritingConventionTest
Assert.Equal(table, StoreObjectIdentifier.Create(owned1EntityType, StoreObjectType.Table)!.Value); Assert.Equal(table, StoreObjectIdentifier.Create(owned1EntityType, StoreObjectType.Table)!.Value);
Assert.Equal(table, StoreObjectIdentifier.Create(owned2EntityType, StoreObjectType.Table)!.Value); Assert.Equal(table, StoreObjectIdentifier.Create(owned2EntityType, StoreObjectType.Table)!.Value);
Assert.Equal("owned_owned_property", owned1EntityType.FindProperty(nameof(Owned.OwnedProperty)).GetColumnName(table)); Assert.Equal("owned_owned_property", owned1EntityType.FindProperty(nameof(Owned.OwnedProperty))!.GetColumnName(table));
Assert.Equal("owned2_owned_property", owned2EntityType.FindProperty(nameof(Owned.OwnedProperty)).GetColumnName(table)); Assert.Equal("owned2_owned_property", owned2EntityType.FindProperty(nameof(Owned.OwnedProperty))!.GetColumnName(table));
var (ownerKey, owned1Key, owned2Key) = var (ownerKey, owned1Key, owned2Key) =
(ownerEntityType.FindPrimaryKey(), owned1EntityType.FindPrimaryKey(), owned1EntityType.FindPrimaryKey()); (ownerEntityType.FindPrimaryKey()!, owned1EntityType.FindPrimaryKey()!, owned1EntityType.FindPrimaryKey()!);
Assert.Equal("pk_destination_table", ownerKey.GetName()); Assert.Equal("pk_destination_table", ownerKey.GetName());
Assert.Equal("pk_destination_table", owned1Key.GetName()); Assert.Equal("pk_destination_table", owned1Key.GetName());
Assert.Equal("pk_destination_table", owned2Key.GetName()); Assert.Equal("pk_destination_table", owned2Key.GetName());
@@ -440,23 +511,23 @@ public class NameRewritingConventionTest
var model = BuildModel(b => var model = BuildModel(b =>
b.Entity<Owner>().OwnsOne(o => o.Owned).ToTable("another_table")); b.Entity<Owner>().OwnsOne(o => o.Owned).ToTable("another_table"));
var ownedEntityType = model.FindEntityType(typeof(Owned)); var ownedEntityType = model.FindEntityType(typeof(Owned))!;
Assert.Equal("pk_another_table", ownedEntityType.FindPrimaryKey().GetName()); Assert.Equal("pk_another_table", ownedEntityType.FindPrimaryKey()!.GetName());
Assert.Equal("another_table", ownedEntityType.GetTableName()); Assert.Equal("another_table", ownedEntityType.GetTableName());
Assert.Equal("owned_property", ownedEntityType.FindProperty("OwnedProperty") Assert.Equal("owned_property", ownedEntityType.FindProperty("OwnedProperty")!
.GetColumnName(StoreObjectIdentifier.Create(ownedEntityType, StoreObjectType.Table)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(ownedEntityType, StoreObjectType.Table)!.Value));
} }
[Fact] [Fact]
public void Owned_entity_withs_OwnsMany() public void Owned_entity_withs_OwnsMany()
{ {
var model = BuildModel(b => b.Entity<Blog>().OwnsMany(b => b.Posts)); var model = BuildModel(mb => mb.Entity<Blog>().OwnsMany(b => b.Posts));
var ownedEntityType = model.FindEntityType(typeof(Post)); var ownedEntityType = model.FindEntityType(typeof(Post))!;
Assert.Equal("post", ownedEntityType.GetTableName()); Assert.Equal("post", ownedEntityType.GetTableName());
Assert.Equal("pk_post", ownedEntityType.FindPrimaryKey().GetName()); Assert.Equal("pk_post", ownedEntityType.FindPrimaryKey()!.GetName());
Assert.Equal("post_title", ownedEntityType.FindProperty("PostTitle") Assert.Equal("post_title", ownedEntityType.FindProperty("PostTitle")!
.GetColumnName(StoreObjectIdentifier.Create(ownedEntityType, StoreObjectType.Table)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(ownedEntityType, StoreObjectType.Table)!.Value));
} }
@@ -486,10 +557,10 @@ public class NameRewritingConventionTest
Assert.Null); Assert.Null);
} }
private IEntityType BuildEntityType(Action<ModelBuilder> builderAction, CultureInfo culture = null) private IEntityType BuildEntityType(Action<ModelBuilder> builderAction, CultureInfo? culture = null)
=> BuildModel(builderAction, culture).GetEntityTypes().Single(); => BuildModel(builderAction, culture).GetEntityTypes().Single();
private IModel BuildModel(Action<ModelBuilder> builderAction, CultureInfo culture = null) private IModel BuildModel(Action<ModelBuilder> builderAction, CultureInfo? culture = null)
{ {
var conventionSet = SqliteTestHelpers var conventionSet = SqliteTestHelpers
.Instance .Instance
@@ -517,15 +588,15 @@ public class NameRewritingConventionTest
public class Blog public class Blog
{ {
public int BlogId { get; set; } public int BlogId { get; set; }
public List<Post> Posts { get; set; } public required List<Post> Posts { get; set; }
} }
public class Post public class Post
{ {
public int PostId { get; set; } public int PostId { get; set; }
public Blog Blog { get; set; } public required Blog Blog { get; set; }
public int BlogId { get; set; } public int BlogId { get; set; }
public string PostTitle { get; set; } public required string PostTitle { get; set; }
} }
public class Parent public class Parent
@@ -539,10 +610,21 @@ public class NameRewritingConventionTest
public int ChildProperty { get; set; } public int ChildProperty { get; set; }
} }
public abstract class AbstractParent
{
public int Id { get; set; }
public int ParentProperty { get; set; }
}
public class ChildOfAbstract : AbstractParent
{
public int ChildProperty { get; set; }
}
public class ChildWithOwned : Parent public class ChildWithOwned : Parent
{ {
public int ChildProperty { get; set; } public int ChildProperty { get; set; }
public Owned Owned { get; set; } public required Owned Owned { get; set; }
} }
public class Split1 public class Split1
@@ -551,7 +633,7 @@ public class NameRewritingConventionTest
public int OneProp { get; set; } public int OneProp { get; set; }
public int Common { get; set; } public int Common { get; set; }
public Split2 S2 { get; set; } public required Split2 S2 { get; set; }
} }
public class Split2 public class Split2
@@ -560,16 +642,16 @@ public class NameRewritingConventionTest
public int TwoProp { get; set; } public int TwoProp { get; set; }
public int Common { get; set; } public int Common { get; set; }
public Split1 S1 { get; set; } public required Split1 S1 { get; set; }
} }
public class Owner public class Owner
{ {
public int Id { get; set; } public int Id { get; set; }
public int OwnerProperty { get; set; } public int OwnerProperty { get; set; }
public Owned Owned { get; set; } public required Owned Owned { get; set; }
[NotMapped] [NotMapped]
public Owned Owned2 { get; set; } public required Owned Owned2 { get; set; }
} }
public class Owned public class Owned

View File

@@ -3,7 +3,6 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<VersionPrefix>7.0.2</VersionPrefix> <VersionPrefix>7.0.2</VersionPrefix>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<SignAssembly>true</SignAssembly> <SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>../EFCore.NamingConventions.snk</AssemblyOriginatorKeyFile> <AssemblyOriginatorKeyFile>../EFCore.NamingConventions.snk</AssemblyOriginatorKeyFile>

View File

@@ -34,7 +34,7 @@ public class NameRewritingConvention :
// Note that the base type is null when the entity type is first added - a base type only gets added later // Note that the base type is null when the entity type is first added - a base type only gets added later
// (see ProcessEntityTypeBaseTypeChanged). But we still have this check for safety. // (see ProcessEntityTypeBaseTypeChanged). But we still have this check for safety.
if (entityType.BaseType is null) if (entityType.BaseType is null && !entityType.ClrType.IsAbstract)
{ {
if (entityType.GetTableName() is { } tableName) if (entityType.GetTableName() is { } tableName)
{ {
@@ -57,10 +57,10 @@ public class NameRewritingConvention :
{ {
var entityType = entityTypeBuilder.Metadata; var entityType = entityTypeBuilder.Metadata;
if (newBaseType is null) if (newBaseType is null || entityType.GetMappingStrategy() == RelationalAnnotationNames.TpcMappingStrategy)
{ {
// The entity is getting removed from a hierarchy. Set the (rewritten) TableName. // The entity is getting removed from a hierarchy. Set the (rewritten) TableName.
if (entityType.GetTableName() is { } tableName) if (entityType.GetTableName() is { } tableName && !entityType.ClrType.IsAbstract)
{ {
entityTypeBuilder.ToTable(_namingNameRewriter.RewriteName(tableName), entityType.GetSchema()); entityTypeBuilder.ToTable(_namingNameRewriter.RewriteName(tableName), entityType.GetSchema());
} }
@@ -71,6 +71,7 @@ public class NameRewritingConvention :
// If this is TPH, we remove the previously rewritten TableName (and non-rewritten Schema) which we set when the // If this is TPH, we remove the previously rewritten TableName (and non-rewritten Schema) which we set when the
// entity type was first added to the model (see ProcessEntityTypeAdded). // entity type was first added to the model (see ProcessEntityTypeAdded).
// If this is TPT, TableName and Schema are set explicitly, so the following will be ignored. // If this is TPT, TableName and Schema are set explicitly, so the following will be ignored.
// TPC is handled above (we need to rewrite just like with a normal table that isn't in an inheritance hierarchy)
entityTypeBuilder.HasNoAnnotation(RelationalAnnotationNames.TableName); entityTypeBuilder.HasNoAnnotation(RelationalAnnotationNames.TableName);
entityTypeBuilder.HasNoAnnotation(RelationalAnnotationNames.Schema); entityTypeBuilder.HasNoAnnotation(RelationalAnnotationNames.Schema);
} }