EF pluralize table's name on generating database from model
Asked Answered
A

4

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 ?

Apterygial answered 20/6, 2013 at 17:37 Comment(6)
See edmx files options (F4): Automatically pluralize.Laughter
@Laughter I can't find edmx options in VS 2010.Apterygial
@Laughter I disabled it but again same problemApterygial
I want only working answer!Apterygial
Hey I'm glad you found a solution, I was worry you didn't. Cheers!Laughter
@Laughter my problem exactly not solved!Apterygial
C
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

Chud answered 5/7, 2013 at 13:36 Comment(11)
not works! It not recognize DbModelBuilder also when I add your namespace.Apterygial
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 ?Chud
I'm using VS2010 but I don't know which version of EF.How can I know it?Apterygial
Thanks.. I have updated the code in my answer ,can you please try thatChud
again not works. it not recognize PluralizingTableNameConvention and ModelBuilder.Apterygial
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 msdn.microsoft.com/en-us/library/wkze6zky(v=vs.80).aspxChud
+1 for your try.I added assembly but on adding using System.Data.Entity.ModelConfiguration.Conventions; says that ModelConfiguration does not exist .Apterygial
I think we are very near , here is the solution ienablemuch.com/2011/04/…Chud
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 worksChud
You really need to determine which version of EF you are using. This changed from ModelBuilder to DbModelBuilder between CTP5 and 4.1.Oomph
+1 - This answer is correct. @majidgeek - You need to also include using System.Data.Entity.Biggers
C
2

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

Cellarer answered 19/11, 2013 at 10:32 Comment(0)
Z
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.
Zebrass answered 9/7, 2013 at 20:38 Comment(0)
M
-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).

Mun answered 8/7, 2013 at 8:20 Comment(2)
it says that does not exist in contextApterygial
it happened for me when i had the previous version of Visual Studio, you can try something like (ContextName.TableName) something like this to refer to the tableMun

© 2022 - 2024 — McMap. All rights reserved.