.NET EF 6 Pluralization with prefix in table names

久未见 提交于 2019-12-02 06:41:51

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.

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