Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use EFCore.NamingConventions to rewrite the name #6287

Closed
wants to merge 4 commits into from
Closed

Use EFCore.NamingConventions to rewrite the name #6287

wants to merge 4 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Nov 20, 2020

I'm trying to fix the issues #2131 & #2351 , use EFCore.NamingConventions to rewrite the name. Supported naming conventions: Default, SnakeCase, LowerCase, UpperCase, UpperSnakeCase.

Changed:
Add Volo.Abp.EntityFrameworkCore reference to EFCore.NamingConventions.
In the xxxDbContext.cs , enable rewrite name by the extension methods NamingConventionsRewriteName of class Volo.Abp.EntityFrameworkCore.DbNamingConventionRewriterExtensions.cs

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)  
        {  
            base.OnConfiguring(optionsBuilder);
     --->       optionsBuilder.NamingConventionsRewriteName(BackgroundJobsDbProperties.DbNamingConvention);
        }
  
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            builder.ConfigureBackgroundJobs();
      --->   builder.NamingConventionsRewriteName(BackgroundJobsDbProperties.DbNamingConvention);
        }

Use outside the abp framework, set the name convensions at the beginning.

AbpCommonDbProperties.DbNamingConvention = "SnakeCase";  
AbpIdentityServerDbProperties.DbNamingConvention = "SnakeCase";

@hikalkan
Copy link
Member

Thanks @scymen. We will check the PR.

@maliming maliming added this to the 4.1-preview milestone Nov 23, 2020
Comment on lines +38 to +61
public static void NamingConventionsRewriteName(this DbContextOptionsBuilder optionsBuilder,
string namingConvention = nameof(DbNamingConvention.Default),
CultureInfo culture = null)
{
switch (namingConvention)
{
case nameof(DbNamingConvention.Default):
break;
case nameof(DbNamingConvention.SnakeCase):
optionsBuilder.UseSnakeCaseNamingConvention(culture);
break;
case nameof(DbNamingConvention.LowerCase):
optionsBuilder.UseLowerCaseNamingConvention(culture);
break;
case nameof(DbNamingConvention.UpperCase):
optionsBuilder.UseUpperCaseNamingConvention(culture);
break;
case nameof(DbNamingConvention.UpperSnakeCase):
optionsBuilder.UseUpperSnakeCaseNamingConvention(culture);
break;
default:
break;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can try to convert (namingConvention )stringto DbNamingConvention, and then reuse NamingConventionsRewriteName method.

Comment on lines +63 to +134
public static void NamingConventionsRewriteName(this ModelBuilder modelBuilder,
string namingConvention = nameof(DbNamingConvention.Default),
CultureInfo culture = null)
{
if (namingConvention == nameof(DbNamingConvention.Default))
{
return;
}

if (Enum.TryParse(namingConvention, out DbNamingConvention naming))
{
NamingConventionsRewriteName(modelBuilder, naming, culture);
}
}

public static void NamingConventionsRewriteName(this ModelBuilder modelBuilder,
DbNamingConvention namingConvention = DbNamingConvention.Default,
CultureInfo culture = null)
{
if (namingConvention == DbNamingConvention.Default)
{
return;
}

INameRewriter nameRewriter = null;
culture = culture ?? CultureInfo.InvariantCulture;
switch (namingConvention)
{
case DbNamingConvention.Default:
break;
case DbNamingConvention.SnakeCase:
nameRewriter = new SnakeCaseNameRewriter(culture);
break;
case DbNamingConvention.LowerCase:
nameRewriter = new LowerCaseNameRewriter(culture);
break;
case DbNamingConvention.UpperCase:
nameRewriter = new UpperCaseNameRewriter(culture);
break;
case DbNamingConvention.UpperSnakeCase:
nameRewriter = new UpperSnakeCaseNameRewriter(culture);
break;
default:
break;
}

foreach (var entity in modelBuilder.Model.GetEntityTypes())
{
entity.SetTableName(nameRewriter.RewriteName(entity.GetTableName()));

foreach (var property in entity.GetProperties())
{
property.SetColumnName(nameRewriter.RewriteName(property.GetColumnName()));
}

foreach (var key in entity.GetKeys())
{
key.SetName(nameRewriter.RewriteName(key.GetName()));
}

foreach (var key in entity.GetForeignKeys())
{
key.SetConstraintName(nameRewriter.RewriteName(key.GetConstraintName()));
}

foreach (var index in entity.GetIndexes())
{
index.SetName(nameRewriter.RewriteName(index.GetName()));
}
}

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

///
/// Default value: "Default".
/// </summary>
public static string DbNamingConvention = "Default";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use enum DbNamingConvention as type will better.

@maliming
Copy link
Member

By the way, there are some bugs in EFCore.NamingConventions. We have to wait for its official version.

https://github.com/efcore/EFCore.NamingConventions/issues?q=is%3Aissue+label%3Abug+milestone%3A5.0.0

@ghost ghost closed this Dec 8, 2020
@ghost ghost deleted the add-naming-convensions-rewriter branch December 8, 2020 11:58
@ghost ghost mentioned this pull request Feb 24, 2021
@danvic712
Copy link

Hi, hasn't this feature been officially released? I am using version 4.2.2 now and have not found this feature yet

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants