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

User-defined rewriting conventions #42

Open
erte12 opened this issue Nov 1, 2020 · 9 comments · May be fixed by #189
Open

User-defined rewriting conventions #42

erte12 opened this issue Nov 1, 2020 · 9 comments · May be fixed by #189
Milestone

Comments

@erte12
Copy link

erte12 commented Nov 1, 2020

Hi,
I used to work with convention like this. Let's say we have entitites:

public class Subject {
    public int Id;
    public string Name;
    public string Surname;
    public PublicProfile RelProfile;
    public DeclarationItem RelDeclaration;
}
public class PublicProfile {
    public int Id;
    public string Name;
    public ICollection<Subject> RelSubjects;
}
public class DeclarationItem {
    public int Id;
    public Name;
    public ICollection<Subject> RelSubjects;
}

It should produce this data structure
Table: dbo.SUBJECT, columns:
S_ID,
S_NAME,
S_SURNAME,
S_REL_PUBLIC_PROFILE,
S_REL_DECLARATION_ITEM

Table: dbo.PUBLIC_PROFILE.
PP_ID
PP_NAME

Table: dbo.DECLARATION_ITEM.
DI_ID,
DI_NAME

Rules:
-Everything should be uppercase,
-Words are joined with _ ,
-All columns starts with prefix that consists of first letters of words that are parts of entity name.

This convention makes querying database very easy. For example usually when you have different prefixes in tables you can write joins without introducing aliases.

Is there a chance to add this convention?

@YohDeadfall
Copy link
Contributor

It's more a personal naming convention than widely used. In such cases it's better to implement it yourself rather to include into a common library.

@roji roji changed the title Convention idea User-defined rewriting conventions Nov 26, 2020
@roji
Copy link
Member

roji commented Nov 26, 2020

What we could do, is to provide a hook to allow users to provide their own rewriters.

@PeteW
Copy link

PeteW commented Feb 16, 2021

Seconded. I originally browsed this library with the hope to find a concise single extension method or class to customize naming conventions. After looking at the code it seems that building an IDbContextOptionsExtension requires a non-trivial amount of internal knowledge of EFCore. And by building this customization you own the long-term responsibility of compatibility in a framework known to change. Wouldnt supporting this be as simple as adding an option for a custom INameRewriter?

@roji
Copy link
Member

roji commented Feb 16, 2021

Wouldnt supporting this be as simple as adding an option for a custom INameRewriter?

Probably... It indeed shouldn't be very hard.

@PeteW
Copy link

PeteW commented Feb 16, 2021

I ended up replacing the library with this

    public class CustomNameSqlGenerationHelper : RelationalSqlGenerationHelper
    {
        private static string Customize(string input) => input.ToLower();
        public CustomNameSqlGenerationHelper([NotNull] RelationalSqlGenerationHelperDependencies dependencies) : base(dependencies) { }

        public override string DelimitIdentifier(string identifier) => base.DelimitIdentifier(Customize(identifier));

        public override void DelimitIdentifier(StringBuilder builder, string identifier) => base.DelimitIdentifier(builder, Customize(identifier));
    }

...
                            optionsBuilder.UseNpgsql(
                                _configuration.GetConnectionString(_configurationService.ApplicationConnectionString),
                                options => { options.EnableRetryOnFailure(5); });
                            optionsBuilder.ReplaceService<ISqlGenerationHelper, CustomNameSqlGenerationHelper>();

@roji
Copy link
Member

roji commented Feb 22, 2021

@PeteW that's a possible approach, though it's hacky and wouldn't work like a full solution. Customizing DelimitIdentifier only controls how EF Core sends table/column names to the database - but is not taken into account for EF Core's model. For example, this means that no migrations will be generated for renaming everything when you activate the above. Logging of table/column names (where DelimitIdentifier isn't called) would also not be affected.

However, if you're OK with these limitations, that approach should work too.

@pfeigl
Copy link

pfeigl commented Jan 26, 2022

Just to make sure: Defining custom naming conventions is still not possible, right?

@roji
Copy link
Member

roji commented Jan 26, 2022

Right.

@pfeigl
Copy link

pfeigl commented Jan 26, 2022

Thanks for the confirmation. Two more questions: We postfix our entities with "Entity" and obviously want to get rid of that for the table names. Is this something, that would be a good match for a custom naming convention or should that be solved differently?

Also how does the IPluralizer and the naming conventions interact with eachother?

If our usecase would be reasonable for naming conventions, I'd happily provide a PR to add support for custom naming rules.

@myieye myieye linked a pull request Feb 23, 2023 that will close this issue
@roji roji added this to the Backlog milestone Jan 3, 2024
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 a pull request may close this issue.

5 participants