mirror of
https://github.com/fergalmoran/EFCore.NamingConventions.git
synced 2025-12-22 09:38:21 +00:00
Revert rewritten name when entity becomes owned
This prevented table splitting from working. Fixes #4
This commit is contained in:
@@ -14,6 +14,9 @@ namespace EFCore.NamingConventions.Test
|
||||
|
||||
public DbSet<SimpleBlog> Blog { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
=> modelBuilder.Entity<SimpleBlog>().OwnsOne(p => p.OwnedStatistics);
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
=> _useNamingConvention(optionsBuilder.UseInMemoryDatabase("test"));
|
||||
}
|
||||
@@ -22,6 +25,13 @@ namespace EFCore.NamingConventions.Test
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string FullName { get; set; }
|
||||
|
||||
public OwnedStatistics OwnedStatistics { get; set; }
|
||||
}
|
||||
|
||||
public class OwnedStatistics
|
||||
{
|
||||
public int SomeStatistic { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,14 @@ namespace EFCore.NamingConventions.Test
|
||||
Assert.Equal("full_name", entityType.FindProperty("FullName").GetColumnName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Owned_entity_name_is_correct_when_configured()
|
||||
{
|
||||
using var context = CreateContext();
|
||||
var entityType = context.Model.FindEntityType(typeof(OwnedStatistics));
|
||||
Assert.Equal("simple_blog", entityType.GetTableName());
|
||||
}
|
||||
|
||||
TestContext CreateContext() => new TestContext(NamingConventionsExtensions.UseSnakeCaseNamingConvention);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,8 @@ namespace EFCore.NamingConventions.Internal
|
||||
/// <summary>
|
||||
/// This class only required so we can have common superclass for all name rewriters
|
||||
/// </summary>
|
||||
internal abstract class NameRewriterBase : IEntityTypeAddedConvention, IPropertyAddedConvention
|
||||
internal abstract class NameRewriterBase : IEntityTypeAddedConvention, IPropertyAddedConvention,
|
||||
IForeignKeyOwnershipChangedConvention
|
||||
{
|
||||
public virtual void ProcessEntityTypeAdded(
|
||||
IConventionEntityTypeBuilder entityTypeBuilder, IConventionContext<IConventionEntityTypeBuilder> context)
|
||||
@@ -20,6 +21,17 @@ namespace EFCore.NamingConventions.Internal
|
||||
=> propertyBuilder.HasColumnName(
|
||||
RewriteName(propertyBuilder.Metadata.GetColumnName()));
|
||||
|
||||
public void ProcessForeignKeyOwnershipChanged(
|
||||
IConventionRelationshipBuilder relationshipBuilder,
|
||||
IConventionContext<IConventionRelationshipBuilder> context)
|
||||
{
|
||||
if (relationshipBuilder.Metadata.IsOwnership)
|
||||
{
|
||||
// Unset the table name which we've set when the entity type was added
|
||||
relationshipBuilder.Metadata.DeclaringEntityType.SetTableName(null);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract string RewriteName(string name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ namespace EFCore.NamingConventions.Internal
|
||||
|
||||
conventionSet.EntityTypeAddedConventions.Add(nameRewriter);
|
||||
conventionSet.PropertyAddedConventions.Add(nameRewriter);
|
||||
conventionSet.ForeignKeyOwnershipChangedConventions.Add(nameRewriter);
|
||||
return conventionSet;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user