0

I came across this neat feature: How to Add Table Prefix In Entity Framework Code First Globally?

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //prefix "PE" on table name
        modelBuilder.Types().Configure(entity => entity.ToTable("PE" + entity.ClrType.Name));
    }

But this loses the pluralisation functionality which I want. I can't just append "s" because a type name like "puppy" should pluralise to "puppies" not "puppys".

Community
  • 1
  • 1
Mr. Boy
  • 60,845
  • 93
  • 320
  • 589

1 Answers1

1

You can call the pluralization service that the underlying EF convention is using:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    //prefix "PE" on table name
    var pluralizationService = DbConfiguration.DependencyResolver.GetService<System.Data.Entity.Infrastructure.Pluralization.IPluralizationService>();
    modelBuilder.Types().Configure(entity => entity.ToTable("PE" + pluralizationService.Pluralize(entity.ClrType.Name)));
}

And don't forget to add using System.Data.Entity.Infrastructure.DependencyResolution;

Florian Haider
  • 1,892
  • 18
  • 23