efcore / EFCore.NamingConventions

Entity Framework Core plugin to apply naming conventions to table and column names (e.g. snake_case)
Apache License 2.0
716 stars 73 forks source link

MaxIdentifierLength is not supported #192

Open gscpw opened 1 year ago

gscpw commented 1 year ago

I use MaxIdentifierLength limit to support Oracle:

public class OpenIddictDbContext : DbContext
{
    public OpenIddictDbContext(DbContextOptions options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // Oracle limit
        modelBuilder.Model.SetMaxIdentifierLength(30);
    }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
        => optionsBuilder.UseSnakeCaseNamingConvention();
}

However, it seems setting the snake_case naming convention ignores the max identifier length and instead generates full-length identifiers. If I do not add snake_case naming convention, the identifiers are limited to 30 chars and are protected against conflict by appending ~, ~1, ~2, etc.

stamahto commented 1 year ago

Hello, any progress? I have the same problem

roji commented 1 year ago

No progress as of now - I'll probably concentrate on the plugin a bit later in the release, before 8.0 is released.

roji commented 8 months ago

This seems to be specifically a problem when the table name is derived from a DbSet property name; otherwise, if there's no DbSet property, the table name is truncated properly (property names are also truncated).

@AndriySvyryd is it safe to assume that this is a conflict/ordering issue between the convention that sets the table name from the DbSet and the naming rewriting convention?

Repro ```c# public class BlogContext : DbContext { // Commenting out the following makes truncation work public DbSet StudentsWithAnotherLongName { get; set; } = null!; protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder .UseNpgsql("Host=localhost;Username=test;Password=test") .UseSnakeCaseNamingConvention() .LogTo(Console.WriteLine, LogLevel.Information) .EnableSensitiveDataLogging(); protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Model.SetMaxIdentifierLength(10); modelBuilder.Entity(); } } public class StudentWithAVeryLongNameExceedingTheMaximum { public int Id { get; set; } public string? PropertyNameThatIsVeryLong1 { get; set; } public string? PropertyNameThatIsVeryLong2 { get; set; } } ```
AndriySvyryd commented 8 months ago

There's definitely a conflict, but I couldn't say where the fix for it should be without investigating the root cause.