Fix owner entity table name resetting

This commit is contained in:
Shay Rojansky
2020-06-17 23:48:05 +02:00
parent 40929d904f
commit 24b3195804
3 changed files with 43 additions and 12 deletions

View File

@@ -18,7 +18,8 @@ namespace EFCore.NamingConventions.Test
=> modelBuilder.Entity<SimpleBlog>(e =>
{
e.HasIndex(b => b.FullName);
e.OwnsOne(b => b.OwnedStatistics);
e.OwnsOne(b => b.OwnedStatistics1);
e.OwnsOne(b => b.OwnedStatistics2, s => s.ToTable("OwnedStatisticsSplit"));
e.HasAlternateKey(b => b.SomeAlternativeKey);
});
@@ -34,7 +35,8 @@ namespace EFCore.NamingConventions.Test
public List<Post> Posts { get; set; }
public OwnedStatistics OwnedStatistics { get; set; }
public OwnedStatistics1 OwnedStatistics1 { get; set; }
public OwnedStatistics2 OwnedStatistics2 { get; set; }
}
public class Post
@@ -46,7 +48,12 @@ namespace EFCore.NamingConventions.Test
public SimpleBlog Blog { get; set; }
}
public class OwnedStatistics
public class OwnedStatistics1
{
public int SomeStatistic { get; set; }
}
public class OwnedStatistics2
{
public int SomeStatistic { get; set; }
}

View File

@@ -43,11 +43,23 @@ namespace EFCore.NamingConventions.Test
}
[Fact]
public void Owned_entity_name_is_correct_when_configured()
public void Owned_entity_is_rewritten()
{
using var context = CreateContext();
var entityType = context.Model.FindEntityType(typeof(OwnedStatistics));
var entityType = context.Model.FindEntityType(typeof(OwnedStatistics1));
Assert.Equal("simple_blog", entityType.GetTableName());
var property = entityType.GetProperty(nameof(OwnedStatistics1.SomeStatistic));
Assert.Equal("some_statistic", property.GetColumnName());
}
[Fact]
public void Owned_entity_split_is_rewritten()
{
using var context = CreateContext();
var entityType = context.Model.FindEntityType(typeof(OwnedStatistics2));
Assert.Equal("OwnedStatisticsSplit", entityType.GetTableName());
var property = entityType.GetProperty(nameof(OwnedStatistics2.SomeStatistic));
Assert.Equal("some_statistic", property.GetColumnName());
}
[Fact]

View File

@@ -1,4 +1,6 @@
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
@@ -15,12 +17,12 @@ namespace EFCore.NamingConventions.Internal
public virtual void ProcessEntityTypeAdded(
IConventionEntityTypeBuilder entityTypeBuilder, IConventionContext<IConventionEntityTypeBuilder> context)
{
var entityType = entityTypeBuilder.Metadata;
// Only touch root entities for now (TPH). Revisit for TPT/TPC.
if (entityTypeBuilder.Metadata.GetRootType() == entityTypeBuilder.Metadata)
if (entityType.BaseType == null)
{
entityTypeBuilder.ToTable(
RewriteName(entityTypeBuilder.Metadata.GetTableName()),
entityTypeBuilder.Metadata.GetSchema());
entityTypeBuilder.ToTable(RewriteName(entityType.GetTableName()), entityType.GetSchema());
}
}
@@ -31,10 +33,20 @@ namespace EFCore.NamingConventions.Internal
public void ProcessForeignKeyOwnershipChanged(IConventionForeignKeyBuilder relationshipBuilder, IConventionContext<bool?> context)
{
if (relationshipBuilder.Metadata.IsOwnership)
var foreignKey = relationshipBuilder.Metadata;
if (foreignKey.IsOwnership)
{
// Unset the table name which we've set when the entity type was added
relationshipBuilder.Metadata.DeclaringEntityType.SetTableName(null);
// Reset the table name which we've set when the entity type was added
// If table splitting was configured by explicitly setting the table name, the following
// does nothing.
foreignKey.DeclaringEntityType.SetTableName(foreignKey.DeclaringEntityType.GetDefaultTableName());
// Also need to reset all primary key properties
foreach (var keyProperty in foreignKey.DeclaringEntityType.FindPrimaryKey().Properties)
{
keyProperty.SetColumnName(keyProperty.GetDefaultColumnName());
}
}
}