mirror of
https://github.com/fergalmoran/EFCore.NamingConventions.git
synced 2025-12-22 09:38:21 +00:00
@@ -1,3 +1,4 @@
|
||||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Xunit;
|
||||
|
||||
@@ -22,6 +23,30 @@ namespace EFCore.NamingConventions.Test
|
||||
Assert.Equal("fullname", entityType.FindProperty("FullName").GetColumnName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Primary_key_name_is_rewritten()
|
||||
{
|
||||
using var context = CreateContext();
|
||||
var entityType = context.Model.FindEntityType(typeof(SimpleBlog));
|
||||
Assert.Equal("pk_simpleblog", entityType.GetKeys().Single().GetName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Foreign_key_name_is_rewritten()
|
||||
{
|
||||
using var context = CreateContext();
|
||||
var entityType = context.Model.FindEntityType(typeof(Post));
|
||||
Assert.Equal("fk_post_simpleblog_blogid", entityType.GetForeignKeys().Single().GetConstraintName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Index_name_is_rewritten()
|
||||
{
|
||||
using var context = CreateContext();
|
||||
var entityType = context.Model.FindEntityType(typeof(SimpleBlog));
|
||||
Assert.Equal("ix_simpleblog_fullname", entityType.GetIndexes().Single().GetName());
|
||||
}
|
||||
|
||||
TestContext CreateContext() => new TestContext(NamingConventionsExtensions.UseLowerCaseNamingConvention);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace EFCore.NamingConventions.Test
|
||||
@@ -15,7 +16,10 @@ namespace EFCore.NamingConventions.Test
|
||||
public DbSet<SimpleBlog> Blog { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
=> modelBuilder.Entity<SimpleBlog>().OwnsOne(p => p.OwnedStatistics);
|
||||
{
|
||||
modelBuilder.Entity<SimpleBlog>(e => e.HasIndex(b => b.FullName));
|
||||
modelBuilder.Entity<SimpleBlog>().OwnsOne(p => p.OwnedStatistics);
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
=> _useNamingConvention(optionsBuilder.UseInMemoryDatabase("test"));
|
||||
@@ -26,9 +30,20 @@ namespace EFCore.NamingConventions.Test
|
||||
public int Id { get; set; }
|
||||
public string FullName { get; set; }
|
||||
|
||||
public List<Post> Posts { get; set; }
|
||||
|
||||
public OwnedStatistics OwnedStatistics { get; set; }
|
||||
}
|
||||
|
||||
public class Post
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string FullName { get; set; }
|
||||
|
||||
public int BlogId { get; set; }
|
||||
public SimpleBlog Blog { get; set; }
|
||||
}
|
||||
|
||||
public class OwnedStatistics
|
||||
{
|
||||
public int SomeStatistic { get; set; }
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Xunit;
|
||||
|
||||
@@ -30,6 +31,30 @@ namespace EFCore.NamingConventions.Test
|
||||
Assert.Equal("simple_blog", entityType.GetTableName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Primary_key_name_is_rewritten()
|
||||
{
|
||||
using var context = CreateContext();
|
||||
var entityType = context.Model.FindEntityType(typeof(SimpleBlog));
|
||||
Assert.Equal("pk_simple_blog", entityType.GetKeys().Single().GetName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Foreign_key_name_is_rewritten()
|
||||
{
|
||||
using var context = CreateContext();
|
||||
var entityType = context.Model.FindEntityType(typeof(Post));
|
||||
Assert.Equal("fk_post_simple_blog_blog_id", entityType.GetForeignKeys().Single().GetConstraintName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Index_name_is_rewritten()
|
||||
{
|
||||
using var context = CreateContext();
|
||||
var entityType = context.Model.FindEntityType(typeof(SimpleBlog));
|
||||
Assert.Equal("ix_simple_blog_full_name", entityType.GetIndexes().Single().GetName());
|
||||
}
|
||||
|
||||
TestContext CreateContext() => new TestContext(NamingConventionsExtensions.UseSnakeCaseNamingConvention);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Xunit;
|
||||
|
||||
@@ -22,6 +23,30 @@ namespace EFCore.NamingConventions.Test
|
||||
Assert.Equal("FULLNAME", entityType.FindProperty("FullName").GetColumnName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Primary_key_name_is_rewritten()
|
||||
{
|
||||
using var context = CreateContext();
|
||||
var entityType = context.Model.FindEntityType(typeof(SimpleBlog));
|
||||
Assert.Equal("PK_SIMPLEBLOG", entityType.GetKeys().Single().GetName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Foreign_key_name_is_rewritten()
|
||||
{
|
||||
using var context = CreateContext();
|
||||
var entityType = context.Model.FindEntityType(typeof(Post));
|
||||
Assert.Equal("FK_POST_SIMPLEBLOG_BLOGID", entityType.GetForeignKeys().Single().GetConstraintName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Index_name_is_rewritten()
|
||||
{
|
||||
using var context = CreateContext();
|
||||
var entityType = context.Model.FindEntityType(typeof(SimpleBlog));
|
||||
Assert.Equal("IX_SIMPLEBLOG_FULLNAME", entityType.GetIndexes().Single().GetName());
|
||||
}
|
||||
|
||||
TestContext CreateContext() => new TestContext(NamingConventionsExtensions.UseUpperCaseNamingConvention);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
|
||||
|
||||
@@ -7,8 +8,9 @@ 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,
|
||||
IForeignKeyOwnershipChangedConvention
|
||||
internal abstract class NameRewriterBase :
|
||||
IEntityTypeAddedConvention, IPropertyAddedConvention, IForeignKeyOwnershipChangedConvention,
|
||||
IEntityTypePrimaryKeyChangedConvention, IForeignKeyAddedConvention, IIndexAddedConvention
|
||||
{
|
||||
public virtual void ProcessEntityTypeAdded(
|
||||
IConventionEntityTypeBuilder entityTypeBuilder, IConventionContext<IConventionEntityTypeBuilder> context)
|
||||
@@ -18,8 +20,7 @@ namespace EFCore.NamingConventions.Internal
|
||||
|
||||
public virtual void ProcessPropertyAdded(
|
||||
IConventionPropertyBuilder propertyBuilder, IConventionContext<IConventionPropertyBuilder> context)
|
||||
=> propertyBuilder.HasColumnName(
|
||||
RewriteName(propertyBuilder.Metadata.GetColumnName()));
|
||||
=> propertyBuilder.HasColumnName(RewriteName(propertyBuilder.Metadata.GetColumnName()));
|
||||
|
||||
public void ProcessForeignKeyOwnershipChanged(
|
||||
IConventionRelationshipBuilder relationshipBuilder,
|
||||
@@ -32,6 +33,24 @@ namespace EFCore.NamingConventions.Internal
|
||||
}
|
||||
}
|
||||
|
||||
public void ProcessEntityTypePrimaryKeyChanged(
|
||||
IConventionEntityTypeBuilder entityTypeBuilder,
|
||||
IConventionKey newPrimaryKey,
|
||||
IConventionKey previousPrimaryKey, IConventionContext<IConventionKey> context)
|
||||
{
|
||||
newPrimaryKey?.Builder?.HasName(RewriteName(newPrimaryKey.GetName()));
|
||||
}
|
||||
|
||||
public void ProcessForeignKeyAdded(
|
||||
IConventionRelationshipBuilder relationshipBuilder,
|
||||
IConventionContext<IConventionRelationshipBuilder> context)
|
||||
=> relationshipBuilder.HasConstraintName(RewriteName(relationshipBuilder.Metadata.GetConstraintName()));
|
||||
|
||||
public void ProcessIndexAdded(
|
||||
IConventionIndexBuilder indexBuilder,
|
||||
IConventionContext<IConventionIndexBuilder> context)
|
||||
=> indexBuilder.HasName(RewriteName(indexBuilder.Metadata.GetName()));
|
||||
|
||||
protected abstract string RewriteName(string name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,10 @@ namespace EFCore.NamingConventions.Internal
|
||||
conventionSet.EntityTypeAddedConventions.Add(nameRewriter);
|
||||
conventionSet.PropertyAddedConventions.Add(nameRewriter);
|
||||
conventionSet.ForeignKeyOwnershipChangedConventions.Add(nameRewriter);
|
||||
conventionSet.EntityTypePrimaryKeyChangedConventions.Add(nameRewriter);
|
||||
conventionSet.ForeignKeyAddedConventions.Add(nameRewriter);
|
||||
conventionSet.IndexAddedConventions.Add(nameRewriter);
|
||||
|
||||
return conventionSet;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user