EF code-first PluralizingTableNameConvention for ONE DbSet

前端 未结 1 1115
滥情空心
滥情空心 2021-01-04 04:21

How do I toggle this convention PluralizingTableNameConvention for only a single table/DbSet? As far as I can tell, I can only do this to all the DbSets

相关标签:
1条回答
  • 2021-01-04 04:51

    If you have only one entity which is mapped to a table that is not pluralizeed then you can remove the PluralizingTableNameConvention and manually configure the table name of the entity.

    public class MyContext : DbContext
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    
            modelBuilder.Entity<Item>().ToTable("Items");
        }
     }
    

    Or if it is the otherway around

    public class MyContext : DbContext
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            modelBuilder.Entity<Item>().ToTable("Item");
        }
     }
    
    0 讨论(0)
提交回复
热议问题