-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Conversation
Thanks @scymen. We will check the PR. |
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; | ||
} | ||
} |
There was a problem hiding this comment.
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 )string
to DbNamingConvention
, and then reuse NamingConventionsRewriteName
method.
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())); | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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.
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 |
Hi, hasn't this feature been officially released? I am using version 4.2.2 now and have not found this feature yet |
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
Use outside the abp framework, set the name convensions at the beginning.