2

Prior to EF Core 5, I've been able to use this code (pasted below) successfully from this original Stack Overflow post Entity Framework Core RC2 table name pluralization

This allowed me to specify SQL tables which persist my entities to use singular names. This doesn't appear to work after upgrading beyond EF Core 3. Most of the posts in Stack Overflow around this topic are dated many years prior. There is one post in the article linked above dated 1/29/2021 which makes mention of the "-NoPluralize" switch for Scaffold-DbContext, except I'm not scaffolding from an existing DB, I'm using the code-first methodology. This is also mentioned in Microsoft's https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-5.0/breaking-changes#pluralizer

I've also tried upgrading to EF Core 6.0.3 to see if this may have been addressed by chance in this release. Unfortunately no luck.

Stepping across the code for entity.SetTableName, both entity.DisplayName() and entity.ShortName() methods return the proper singular name strings, so I know SetTableName() is receiving the proper string.

Investigating the SetTableName method in the RelationalEntityTypeExtension class in the EF Core source out on GitHub doesn't reveal anything interesting or abnormal, that is if I'm looking at the correct class.

There's bound to be a solution for this, I couldn't possibly be the only one :-) Any suggested articles or techniques to try? The only thing left I can think of to try is to get breakpoints on some of the EF sources with some debug symbols and see what's going on beyond just my code. Any input or guidance would be tremendously helpful!

In my context class, pluralizing invoked by:

protected override void OnModelCreating(ModelBuilder modelBuilder){
  modelBuilder.RemovePluralizingTableNameConvention();
}

In pluralizing method implemented in an extension class:

public static class ModelBuilderExtensions{
 public static void RemovePluralizingTableNameConvention(this ModelBuilder modelBuilder){
  foreach (var entity in modelBuilder.Model.GetEntityTypes()){
    entity.SetTableName(entity.DisplayName());
   }
  }
}
BSmith
  • 163
  • 1
  • 1
  • 7
  • A bunch of api's moved to extension methods in Microsoft.EntityFrameworkCore.Relational. – Jeremy Lakeman Mar 22 '22 at 00:26
  • `SetTableName` is the correct (intended, "by design") way, not sure why it "doesn't work" for you - it definitely does for me (in EFC 3.x, 5.x, 6.x). I'm using `entityType.SetTableName(entityType.GetDefaultTableName())`, but that shouldn't make the difference. – Ivan Stoev Mar 23 '22 at 07:51
  • Interesting, thanks @IvanStoev for the reply. Agreed, the var name shouldn't make a difference. By chance are you running on Win or Mac? I realize that shouldn't matter either, Core is Core, but trying to narrow down what could be different from my implementation (which worked in 3x) from yours, because yours is working. If it's working for you and there aren't a flurry of issues on the EF GitHub issues page, then it has to be something localized in my environment. – BSmith Mar 23 '22 at 17:25
  • I'm with Win10, Net6 Console App, latest EFC 6.0.3, SqlServer (LocalDB). Btw, what doesn't work for you - add-migration or queries? – Ivan Stoev Mar 23 '22 at 19:48
  • I'm running on macOS 11.6.4, using Visual Studio 2019 Community for Mac v8.10.21, Dotnet core v6.0.103, MSSQL 13.0.4001.0 running on a proper Windows server, building an ASP.NET Core v6.0.3 REST API. I'm able to add migrations, start the app up, EF builds the schema fine, with the exception that all my tables are named in plural form with an "s" at the end. At the end of the day I can stomach this and move along, but the fact that there is code that is meant to custom tailor your table names and it isn't working drives me nuts. I'm going to re-target .NET Core 5 just for fun and see – BSmith Mar 23 '22 at 21:10

1 Answers1

1

Ok, well I guess we'll just chalk this one up to general weirdness. @Ivan Stoev, thanks for your patience. I targeted .NET Core 5, EF Core 5.0.15, dropped all tables, rebuilt, ensured .bin was empty, added a new migration, applied the migration, and all the tables were named singular. So, i repeated that exact same process, except this time put everything back to .NET 6, EF 6.0.3. All the tables rebuilt with singular names, which is the desired result. I'm baffled but happy now. Must have been classic PEBKAC :-D

BSmith
  • 163
  • 1
  • 1
  • 7