EF is the name of the plural table when generating the database from the model

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

Option model

Now, when I want to generate the database from the model, it adds 's' to the table name in the generated sql:

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

I also turned off multiple name assignment, but nothing changed:

enter image description here

This causes errors when deploying the web application. How can I prevent pluralization?

+6
source share
4 answers

Just override the OnModelCreating method and remove this "PluralizingTableNameConvention" convention. This way you tell Entity Framework not to expand table names, just add

Updated

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

It will remove the Pluralising agreement, which by default is tied to all model builders.

You also need to add a namespace

 System.Data.Entity.ModelConfiguration.Conventions; 

Hope this helps

+4
source

If you create an EDMX file enter image description here

you must uncheck the pluralize tick checkbox
+1
source

It seems that the pluralization function can only be deactivated with the First code. Therefore, the following code does not work:

 modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 

Try this, perhaps with luck:

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

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

  • In the project tree "Links" and press F4 ont "EntityFramework".
  • Right-click on your project and select "Manage NuGet Packages ...", here you can check the version and update it.
0
source

The reason is this:

any tables created in the database, the Entity Framework converts them into a class, so you can easily create objects from it.

Just check the tables in the code behind as the only ones (exactly as defined by EF).

Be careful! (do not name any page in your application with the same name that you have in any of your tables, because they are both converted to classes, and when you wana the table, they get lost between the table and the page).

-3
source

Source: https://habr.com/ru/post/947766/


All Articles