How do I singularize my tables in EF Code First?

后端 未结 3 1733
一向
一向 2020-11-29 02:36

I prefer using singular nouns when naming my database tables. In EF code first however, the generated tables always are plural. My DbSets are pluralized which I believe is

3条回答
  •  暖寄归人
    2020-11-29 02:50

    You've removed the wrong convention (PluralizingEntitySetNameConvention) for this purpose. Just replace your OnModelCreating method with the below and you will be good to go.

    using System.Data.Entity.ModelConfiguration.Conventions.Edm.Db;
    ...
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {    
        modelBuilder.Conventions.Remove();
    }
    

    With Entity Framework 6, on your file that inherit from DbContext:

    using System.Data.Entity.ModelConfiguration.Conventions;
    
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove();
    }
    

提交回复
热议问题