我正在尝试使用 Entity Framework 5。第一个问题是 EF 自动创建表。我试图通过包含来修复它
dbModelBuilder.Conventions.Remove<PluralizingTableNameConvention>()
。第二个问题是这样的错误
自数据库创建以来,支持“CountryContext”上下文的模型已更改。考虑使用 Code First 迁移来更新数据库。
我尝试修复它,dbModelBuilder.Conventions.Remove<IncludeMetadataConvention>();
但没有任何意义。接下来是数据访问层:
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;
}
}
附加信息:VS 2012,框架 4.5,实体框架 5.0.0.0 而对于 EF 4,它可以完美运行(没有 OnModelCreating 方法)。