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).
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.
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.
[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.
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.
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.
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.
You can write this code in OnModelCreating method:
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With