Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 796
Majid Avatar asked Jun 20 '13 17:06

Majid


1 Answers

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

like image 100
microtechie Avatar answered Oct 10 '22 10:10

microtechie