4

(This is not a dupe, please read my comment.)

I've just migrated from EF Core Preview 5 to Preview 6. This seems to be a breaking change, especially the mapping will break to the existing Databases if this remains in the release version.

In preview 5 I used:

entityType.Relational.TableName = entityType.DisplayName();

Now it seems Relational property was removed. I would not fall back to manually declare the TableName for all dozens of entities, instead just instruct EF Core model builder do not pluralize automatically them.

g.pickardou
  • 32,346
  • 36
  • 123
  • 268
  • Possible duplicate of [Entity Framework Core RC2 table name pluralization](https://stackoverflow.com/questions/37493095/entity-framework-core-rc2-table-name-pluralization) – Jan Paolo Go Jun 20 '19 at 13:49
  • Thx, I reviewed that Q. There almost all answers mention "entity.Relational().TableName =...." as solution. My question is about this API removed and no longer available – g.pickardou Jun 20 '19 at 14:07
  • The API is removed. > "There is no convention for this as of EF RC2 build" – Jan Paolo Go Jun 20 '19 at 14:39
  • Is it DisplayName you are missing? – ErikEJ Jun 20 '19 at 14:56
  • @JanPaoloGo: That API was not removed. Please consider my issue started few days ago with Core 3 preview6 that answer is more than 3 years old. The API I an looking for is existed few days ago. – g.pickardou Jun 20 '19 at 14:56
  • Oh, you're talking about `entityType.Relational.TableName = entityType.DisplayName();`. I was talking about the `PluralizingTableNameConvention`. my bad – Jan Paolo Go Jun 20 '19 at 14:58
  • @ErikEJ: No the .Relational property is missing, (I am going to edit the Q) – g.pickardou Jun 20 '19 at 14:58
  • @JanPaoloGo how can you possibly mark duplicate on a preview 6 question with a 3 year old answer – Avin Kavish Jun 20 '19 at 15:01
  • @AvinKavish: To be fair, my question was not so clear in its original form... now it evolved a bit – g.pickardou Jun 20 '19 at 15:03
  • Using preview (beta) versions is on your own risk. They are expected to have breaking changes or non working things. To me it doesn't worth the effort to search the code base which can be changed at any moment before release. – Ivan Stoev Jun 21 '19 at 15:09
  • @IvanStoev, hopefully we all know that beta versions expected to have breaking changes. I was not complaining. What worth and what not, could be different by person, project, context. Btw my guess this change will remain a breaking in the release. – g.pickardou Jun 22 '19 at 04:56

2 Answers2

3

EF Core 3 introduces, starting preview6, breaking changes on Provider-specific Metadata API. This includes removal of RelationalMetadataExtensions together with its extension methods such as Relational(this IMutableEntityType entityType).

It is replaced by RelationalEntityTypeExtensions where you can do the following:

IMutableEntityType entity = ...;
entity.SetTableName(entity.DisplayName());

With that, removing automatic pluralization can be done as described in this answer on a related question

using Microsoft.EntityFrameworkCore.Metadata;

public static class ModelBuilderExtensions 
{
    public static void RemovePluralizingTableNameConvention(this ModelBuilder modelBuilder)
    {
        foreach (IMutableEntityType entity in modelBuilder.Model.GetEntityTypes())
        {
            entity.SetTableName(entity.DisplayName());
        }
    }
}
Jan Paolo Go
  • 5,842
  • 4
  • 22
  • 50
  • 1
    Thanks. This SetTableName overwrites the [Table("MyTableName")] attribute, back to DisplayName. Is there any way to make this just default (if there is no [Table...] attribute? With other words make the default behaviour no pluralizing, but allow to use [Table...] attribute? – g.pickardou Jun 21 '19 at 09:29
  • @g.pickardou you could use reflection e.g. `entity.ClrType.GetCustomAttribute()?.Name != null` to check if the type has `[Table...]` attribute. – Jan Paolo Go Jun 21 '19 at 12:37
0

Improved version of Jan Paolo Go's Answer This prevents intermediate table to become something like TeacherStudent Dictionary<string, object>

public static class ModelBuilderExtensions 
{
    public static void RemovePluralizingTableNameConvention(this ModelBuilder modelBuilder)
    {
    
        foreach (IMutableEntityType entity in modelBuilder.Model.GetEntityTypes())
        {
           if (entity is EntityType { IsImplicitlyCreatedJoinEntityType: true })
           {
             continue;
           }
           entity.SetTableName(entity.DisplayName());
        }
    }
}
Manuel Amstutz
  • 1,311
  • 13
  • 33