6

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 ?

Majid
  • 13,853
  • 15
  • 77
  • 113

4 Answers4

4

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

Rajeev Bera
  • 2,021
  • 1
  • 16
  • 30
  • not works! It not recognize `DbModelBuilder` also when I add your `namespace`. – Majid Jul 05 '13 at 15:43
  • You need to make sure you add a reference in your project to the the assembly listed at the MSDN link. The assembly is Microsoft.Data.Scheme.dll / Microsoft.Data.Schema.SchemaModel.. May i know which version of EF you are using ? – Rajeev Bera Jul 08 '13 at 09:01
  • I'm using VS2010 but I don't know which version of EF.How can I know it? – Majid Jul 08 '13 at 09:27
  • Thanks.. I have updated the code in my answer ,can you please try that – Rajeev Bera Jul 08 '13 at 09:38
  • again not works. it not recognize `PluralizingTableNameConvention` and `ModelBuilder`. – Majid Jul 08 '13 at 13:14
  • basically it will copies to the bin directory, so you can search in C: drive for Microsoft.Data.Schema.dll and copy into bin folder .. for more details have a look http://msdn.microsoft.com/en-us/library/wkze6zky(v=vs.80).aspx – Rajeev Bera Jul 08 '13 at 14:10
  • +1 for your try.I added assembly but on adding `using System.Data.Entity.ModelConfiguration.Conventions;` says that `ModelConfiguration` does not exist . – Majid Jul 08 '13 at 16:10
  • I think we are very near , here is the solution http://www.ienablemuch.com/2011/04/type-or-namespace-name-modelbuilder.html – Rajeev Bera Jul 08 '13 at 18:46
  • In your code - replace "using System.Data.Entity.DbmodelBuilder" with "using System.Data.Entity.ModelConfiguration.Conventions;" and the methid signature as well .... protected override void OnModelCreating(DbModelBuilder modelBuilder) ...I have updated my original answer, you can see from there..Hope it works – Rajeev Bera Jul 09 '13 at 07:38
  • You really need to determine which version of EF you are using. This changed from ModelBuilder to DbModelBuilder between CTP5 and 4.1. – Nick Jul 09 '13 at 10:19
  • +1 - This answer is correct. @majidgeek - You need to **also** include `using System.Data.Entity`. – Travis J Jul 10 '13 at 02:48
2

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

0

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.
glautrou
  • 3,140
  • 2
  • 29
  • 34
-3

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).