Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How turn off pluralize table creation for Entity Framework 5?

I am trying to use Entity Framework 5. The first problem was that EF creats tables automatically. I tried to fix it by including dbModelBuilder.Conventions.Remove<PluralizingTableNameConvention>(). The second problem was the error like this

The model backing the 'CountryContext' context has changed since the database was created. Consider using Code First Migrations to update the database.

I tried fix it by dbModelBuilder.Conventions.Remove<IncludeMetadataConvention>(); but no sense. The data access layer the next:

Table(Name = "tblCountries")] public class Country {      [Column(Name = "id", IsDbGenerated = true, IsPrimaryKey = true)]     public int Id {get;set;}      [Column(Name = "name")]     public string Name {get;set;} }  public class CountryContext:DbContext {     public CountryContext(string connStr):base(connStr)     {     }      public DbSet<Country> TblCountries { get; set; }      protected override void OnModelCreating(DbModelBuilder dbModelBuilder)     {         dbModelBuilder.Conventions.Remove<PluralizingTableNameConvention>();         dbModelBuilder.Conventions.Remove<IncludeMetadataConvention>();     } }      public class CountryDal:BaseDal {    public int CheckIsExist(Country country)     {         int id = 0;         using (var context = new CountryContext(ConnectionString))         {             var first = context.TblCountries.FirstOrDefault(el => el.Name == country.Name);             if (first != null)             {                 id = first.Id;             }         }         return id;     }     } 

Additional info: VS 2012, framework 4.5, entity framework 5.0.0.0 And for EF 4 it works perfect (without OnModelCreating method).

like image 441
Yara Avatar asked Aug 26 '12 12:08

Yara


People also ask

What is OnModelCreating in Entity Framework?

The DbContext class has a method called OnModelCreating that takes an instance of ModelBuilder as a parameter. This method is called by the framework when your context is first created to build the model and its mappings in memory.

Does Entity Framework create tables?

If you're using a Code First approach then Entity Framework will build the table for you. It looks like you are not using Code First, so you will have create the table in the database.

Does Entity Framework 7 pluralize table names with code?

[SOLVED] => Entity Framework 7 pluralize table names with code... I am new to ASP/EF. I am using ASP 5 and Entity Framework 7 in my personal project. So I am able to create database and tables with code first approach, but all the table names are singular and does not pluralize by default.

How do I remove the pluralizing convention in Entity Framework?

For solving this problem the Entity Framework provides a very good approach, Conventions, that allow you to specify how you want your database to be set up. using System.Data.Entity.ModelConfiguration.Conventions; This code will remove the Pluralizing convention that is the default behavior for all model builders.

What is an example of a pluralized entity in Visual Studio?

For example, adding a Customers table to the O/R Designer results in an entity class named Customer because the class will hold data for only a single customer. Pluralization is on by default only in the English-language version of Visual Studio.

How to create a student table in Entity Framework?

For example, in the Code First approach you made entity (class) named Student and expect the Student Table will be created. But the default table created in the Db will be Students. We will understand it with creating a sample application. Create a sample console application. Then install the Nuget package Entityframework.


1 Answers

You can write this code in OnModelCreating method:

modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 
like image 77
Majid Hosseini Avatar answered Oct 02 '22 21:10

Majid Hosseini