Question

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 ?

Was it helpful?

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

OTHER TIPS

You Should uncheck pluralize tick when you are creating EDMX fileenter image description here

It seems that the pluralization feature can be deactivated with Code First only. That's why the following code doesn't work:

modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

Try this, maybe with luck:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Option>().ToTable("Option")
}

If you need to know which version of Entity Framework you are using there are several ways:

  • In your project tree exapand "References" and press F4 ont "EntityFramework".
  • Right click on your project and chooes "Manage NuGet packages...", from here you can check the version and update it.

The reason is :

any tables you have created in the databse, Entity Framework converts it into a class, so that you can easily create objects out of it.

Just verify the tables in the code behind as singular (just as it was defined by EF).

Be Careful ! (dont name any page in your application with the same name you have in any of your tables, becuase both of them are being converted to classes and when you wana call the table, it will get lost between the table and the page).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top