Tôi đang cố gắng sử dụng Entity Framework 5. Vấn đề đầu tiên là EF tự động tạo bảng. Tôi đã cố khắc phục bằng cách bao gồm dbModelBuilder.Conventions.Remove<PluralizingTableNameConvention>()
. Vấn đề thứ hai là lỗi như thế nàyLàm thế nào tắt việc tạo bảng số nhiều cho Entity Framework 5?
Mô hình ủng hộ bối cảnh 'CountryContext' đã thay đổi kể từ khi cơ sở dữ liệu được tạo. Cân nhắc sử dụng Code First Migrations để cập nhật cơ sở dữ liệu .
Tôi đã cố khắc phục bằng dbModelBuilder.Conventions.Remove<IncludeMetadataConvention>();
nhưng không có ý nghĩa. Lớp truy cập dữ liệu tiếp theo:
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;
}
}
Thông tin thêm: VS 2012, khuôn khổ 4.5, thực thể khuôn khổ 5.0.0.0 Và đối với EF 4 nó hoạt động hoàn hảo (không có phương pháp OnModelCreating).
Hoàn đoán ở đây nhưng không EF vẫn nhận ra ent ity như một định danh khi bạn ngụ ý nó như là 'Id' trái ngược với việc sử dụng ALL CAPS như vậy:' ID' –