EF6中的前缀和复数表名?

时间:2016-07-15 10:46:15

标签: c# entity-framework ef-code-first entity-framework-6

我遇到了这个简洁的功能: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));
    }

但这会失去我想要的复数功能。我无法追加"s",因为类型名称如" puppy"应该多数到小狗"不是" puppys"。

1 个答案:

答案 0 :(得分:1)

您可以调用基础EF约定正在使用的复数服务:

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)));
}

不要忘记添加using System.Data.Entity.Infrastructure.DependencyResolution;