efcore / EFCore.NamingConventions

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

RewriteName Errors With Views That Has Keys #279

Open mattmentrup opened 3 months ago

mattmentrup commented 3 months ago

Core 6 Oracle.EntityFrameworkCore 6 EFCore.NamingConvention, Latest on NuGet.org

Naming Convention Used: UsedSnakeCaseNamingConvetion, UseUpperSnakeCaseNamingConvention

During the OnModelCreating(ModelBuilder), when configuring the modelBuilder with a View and that view has keys defined, the RewriteName function throws an 'object reference not set to an instance of an object' error.

OnModelCreating(builder) {
  builder.EnsureSchema("abc"); // I do not believe this matters, but just in case for testing.

  builder.Entity<DTOEntity>(ef => {
    ef.ToView("xyz");
    ef.HasKey(e => new { e.Key1, e.Key2 }); // Throws error on this line.
  }
}

Interestingly enough, when declaring the ToView after the HasKey method, the error above is not thrown.

For example, the below snippet does not cause the error to occur.

OnModelCreating(builder) {
  builder.EnsureSchema("abc"); // I do not believe this matters, but just in case for testing.

  builder.Entity<DTOEntity>(ef => {
    ef.HasKey(e => new { e.Key1, e.Key2 });
    ef.ToView("xyz");
    // No error is thrown and the extension is happy.
  }
}
mattmentrup commented 3 months ago

I identified the issue that Microsoft EntityFramework will set the Name value for the key after the entity has been declared as a view to null.

I submitted a PR #280 to handle this fix by adjusting the INameRewriter.RewriteName signature to accept the nullable string value and can return a nullable string value.

roji commented 3 months ago

@mattmentrup thanks.. I'm very busy right now with other priorities - I traditionally turn my attention to this plugin close to the release time in November, when other components are already frozen etc. Just to set expectations in terms of reviewing etc - but I'll definitely take a look later in the year.

mattmentrup commented 3 months ago

Not a problem, thanks for maintaining this plugin. Solved my issue with naming schemas for oracle at work.