Rewrite alternative key names

Fixes #18
This commit is contained in:
Shay Rojansky
2020-02-12 14:39:06 +01:00
parent 6ed5416217
commit e49ce80c22
6 changed files with 40 additions and 17 deletions

View File

@@ -28,7 +28,15 @@ namespace EFCore.NamingConventions.Test
{
using var context = CreateContext();
var entityType = context.Model.FindEntityType(typeof(SimpleBlog));
Assert.Equal("pk_simpleblog", entityType.GetKeys().Single().GetName());
Assert.Equal("pk_simpleblog", entityType.GetKeys().Single(k => k.IsPrimaryKey()).GetName());
}
[Fact]
public void Alternative_key_name_is_rewritten()
{
using var context = CreateContext();
var entityType = context.Model.FindEntityType(typeof(SimpleBlog));
Assert.Equal("ak_simpleblog_somealternativekey", entityType.GetKeys().Single(k => !k.IsPrimaryKey()).GetName());
}
[Fact]

View File

@@ -16,10 +16,12 @@ namespace EFCore.NamingConventions.Test
public DbSet<SimpleBlog> Blog { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
=> modelBuilder.Entity<SimpleBlog>(e =>
{
modelBuilder.Entity<SimpleBlog>(e => e.HasIndex(b => b.FullName));
modelBuilder.Entity<SimpleBlog>().OwnsOne(p => p.OwnedStatistics);
}
e.HasIndex(b => b.FullName);
e.OwnsOne(b => b.OwnedStatistics);
e.HasAlternateKey(b => b.SomeAlternativeKey);
});
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> _useNamingConvention(optionsBuilder.UseInMemoryDatabase("test"));
@@ -29,6 +31,7 @@ namespace EFCore.NamingConventions.Test
{
public int Id { get; set; }
public string FullName { get; set; }
public int SomeAlternativeKey { get; set; }
public List<Post> Posts { get; set; }

View File

@@ -36,7 +36,15 @@ namespace EFCore.NamingConventions.Test
{
using var context = CreateContext();
var entityType = context.Model.FindEntityType(typeof(SimpleBlog));
Assert.Equal("pk_simple_blog", entityType.GetKeys().Single().GetName());
Assert.Equal("pk_simple_blog", entityType.GetKeys().Single(k => k.IsPrimaryKey()).GetName());
}
[Fact]
public void Alternative_key_name_is_rewritten()
{
using var context = CreateContext();
var entityType = context.Model.FindEntityType(typeof(SimpleBlog));
Assert.Equal("ak_simple_blog_some_alternative_key", entityType.GetKeys().Single(k => !k.IsPrimaryKey()).GetName());
}
[Fact]

View File

@@ -28,7 +28,15 @@ namespace EFCore.NamingConventions.Test
{
using var context = CreateContext();
var entityType = context.Model.FindEntityType(typeof(SimpleBlog));
Assert.Equal("PK_SIMPLEBLOG", entityType.GetKeys().Single().GetName());
Assert.Equal("PK_SIMPLEBLOG", entityType.GetKeys().Single(k => k.IsPrimaryKey()).GetName());
}
[Fact]
public void Alternative_key_name_is_rewritten()
{
using var context = CreateContext();
var entityType = context.Model.FindEntityType(typeof(SimpleBlog));
Assert.Equal("AK_SIMPLEBLOG_SOMEALTERNATIVEKEY", entityType.GetKeys().Single(k => !k.IsPrimaryKey()).GetName());
}
[Fact]

View File

@@ -10,7 +10,8 @@ namespace EFCore.NamingConventions.Internal
/// </summary>
internal abstract class NameRewriterBase :
IEntityTypeAddedConvention, IPropertyAddedConvention, IForeignKeyOwnershipChangedConvention,
IEntityTypePrimaryKeyChangedConvention, IForeignKeyAddedConvention, IIndexAddedConvention
IKeyAddedConvention, IForeignKeyAddedConvention,
IIndexAddedConvention
{
public virtual void ProcessEntityTypeAdded(
IConventionEntityTypeBuilder entityTypeBuilder, IConventionContext<IConventionEntityTypeBuilder> context)
@@ -33,13 +34,8 @@ namespace EFCore.NamingConventions.Internal
}
}
public void ProcessEntityTypePrimaryKeyChanged(
IConventionEntityTypeBuilder entityTypeBuilder,
IConventionKey newPrimaryKey,
IConventionKey previousPrimaryKey, IConventionContext<IConventionKey> context)
{
newPrimaryKey?.Builder?.HasName(RewriteName(newPrimaryKey.GetName()));
}
public void ProcessKeyAdded(IConventionKeyBuilder keyBuilder, IConventionContext<IConventionKeyBuilder> context)
=> keyBuilder.HasName(RewriteName(keyBuilder.Metadata.GetName()));
public void ProcessForeignKeyAdded(
IConventionRelationshipBuilder relationshipBuilder,

View File

@@ -30,7 +30,7 @@ namespace EFCore.NamingConventions.Internal
conventionSet.EntityTypeAddedConventions.Add(nameRewriter);
conventionSet.PropertyAddedConventions.Add(nameRewriter);
conventionSet.ForeignKeyOwnershipChangedConventions.Add(nameRewriter);
conventionSet.EntityTypePrimaryKeyChangedConventions.Add(nameRewriter);
conventionSet.KeyAddedConventions.Add(nameRewriter);
conventionSet.ForeignKeyAddedConventions.Add(nameRewriter);
conventionSet.IndexAddedConventions.Add(nameRewriter);