How do I split my tables in EF Code first?

I prefer to use singular nouns when naming my database tables. However, in EF code, the generated tables are always plural. My DbSets are pluralized, and I believe that EF generates names, but I do not want to create these names, because I think it is more rational to use them in the plural. I also tried to override the setting, but to no avail.

Any ideas? Here is my code and thanks.

MyObjectContext.cs

public class MyObjectContext : DbContext, IDbContext { public MyObjectContext(string connString) : base(connString) { } public DbSet<Product> Products {get;set;} public DbSet<Category> Categories {get;set;} //etc. protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>(); } } 
+48
entity-framework ef-code-first
Jan 25 2018-11-17T00:
source share
3 answers

For this purpose, you have deleted the wrong agreement (PluralizingEntitySetNameConvention). Just replace the OnModelCreating method below and you will be good to go.

 using System.Data.Entity.ModelConfiguration.Conventions.Edm.Db; ... protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } 

With Entity Framework 6 in your file, which inherits from DbContext:

 using System.Data.Entity.ModelConfiguration.Conventions; protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } 
+80
Jan 25 2018-11-18T00:
source share

You can also change the value of the property:

From the Tools menu, select Options. In the Options dialog box, expand Database Tools. Click O / R Designer. Set the Name Pluralization Enabled = False to set the O / R constructor so that it does not change the class names. Set Name Pluralization to Enabled = True to apply pluralization rules to object class names added to O / R Designer.

+2
Jul 29 '15 at 14:23
source share

The location of the PluralizingTableNameConvention definition has moved to:

using System.Data.Entity.ModelConfiguration.Conventions;

+1
Jun 24 '14 at 5:02
source share



All Articles