Search code examples
sqlvisual-studio-2010entity-frameworkef-model-firstpluralize

EF pluralize table's name on generating database from model


I have some models and tables in EF that you can see one of those here:

Option model

Now when I want to generate database from model it adds 's' to name of tables in generated sql:

CREATE TABLE [dbo].[Options] (
[Id] int IDENTITY(1,1) NOT NULL,
[Name] nvarchar(50)  NOT NULL,
[Price] int  NOT NULL
); 

I also disabled pluralizing of names as this but nothing changed:

enter image description here

This cause errors on deploying web application. How can I prevent pluralizing ?


Solution

  • Just override the OnModelCreating method and remove that “PluralizingTableNameConvention” convention. So you are telling Entity Framework not to pluralise table names, simply add

    Updated

     protected override void OnModelCreating(DbModelBuilder modelBuilder)
     {    
         modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
     }
    

    It will remove the Pluralising convention that is by default attached to all model builders

    Also you need to add a namespace

    System.Data.Entity.ModelConfiguration.Conventions;
    

    Hope it will help