.NET EF 6 Pluralization with prefix in table names

僤鯓⒐⒋嵵緔 提交于 2019-12-20 05:00:11

问题


Plural table names are default convention in EF. but when I have added the prefix I can not make names anymore plural unfortunately. Any ideas?

protected override void OnModelCreating(DbModelBuilder modelBuilder) {
  modelBuilder.Types()
              .Configure(entity => entity.ToTable("MyPrefix_" + entity.ClrType.Name));

  modelBuilder.Conventions.Add<PluralizingTableNameConvention>();
  base.OnModelCreating(modelBuilder);
}

回答1:


The PluralizingTableNameConvention uses a PluralizationService that can be used anywhere. So you can go ahead and use it in your configuration code.

Here is an example using a Model "Person" which should be pluralized to "People":

    public DbSet<Person> Persons { get; set; }

    protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        var serv = PluralizationService.CreateService(new System.Globalization.CultureInfo("en-us"));

        modelBuilder.Types()
         .Configure(entity => entity.ToTable("MyPrefix_" + serv.Pluralize(entity.ClrType.Name)));

        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        base.OnModelCreating(modelBuilder);
    }

After running this code it will correctly pluralize the Persons model to "MyPrefix_People" in the database.

To you use the PluralizationService you will have to reference the System.Data.Entity.Design assembly.



来源:https://stackoverflow.com/questions/26138823/net-ef-6-pluralization-with-prefix-in-table-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!