efcore/EFCore.NamingConventions

Name Preprocessor propose

dukecheng opened this issue · 7 comments

I usually like to add a suffix for Persist Object class naming,
example for UserEntity, and expected table name User, Class Name: UserEntity => TableName: User

So is there have a name preprocessor for this let me to remove the suffix when convert the name,
If doesn't have yet, I can commit a pr to support the NamePreprocessor, do you have any suggest?

roji commented

@dukecheng what's the rule here? When is which suffix supposed to be removed?

I want to let the developer customize the rule,
Example for:

public class CustomizeNamePreprocessor : INamePreprocessor
{
    public const string TableNameSuffix = "Entity";
    public const string ViewNameSuffix = "ViewEntity";
    public string TableName(string name)
    {
        return name.EndsWith(TableNameSuffix) ? name.Substring(0, name.Length - TableNameSuffix.Length) : name;
    }

    public string ViewName(string name)
    {
        return name.EndsWith(ViewNameSuffix) ? name.Substring(0, name.Length - ViewNameSuffix.Length) : name;
    }
}

I suggest to add a NamingConventionOptions to configure it, and pass it into the Use extensions class

public class NamingConventionOptions
{
    public INamePreprocessor NamePreprocessor { get; set; } = new NullNamePreprocessor();
}
public static DbContextOptionsBuilder UseSnakeCaseNamingConvention(
    [NotNull] this DbContextOptionsBuilder optionsBuilder, CultureInfo culture = null, Action<NamingConventionOptions> options = null)
{
    Check.NotNull(optionsBuilder, nameof(optionsBuilder));

    NamingConventionOptions conventionOptions = new NamingConventionOptions();
    options?.Invoke(conventionOptions);

    var extension = (optionsBuilder.Options.FindExtension<NamingConventionsOptionsExtension>()
            ?? new NamingConventionsOptionsExtension())
        .WithSnakeCaseNamingConvention(conventionOptions);

    ((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);

    return optionsBuilder;
}
roji commented

@dukecheng that sounds like a dup of #42

Thanks for your quickly response,

Yes, looks there are same, Is anybody work on this for now and how is the progress?

roji commented

Duplicate of #42

roji commented

Nobody's working on that for now. I'm heads-down working on EF Core 7.0 and related urgent things, typically shortly before or after the release I do a work cycle on this plugin (at the very least to make it compatible with the new EF Core release). I might be able to do that as part of that.

Sounds good, I am looking forward to it