gpt4 book ai didi

c# - 如何关闭 Entity Framework 5 的复数表创建?

转载 作者:IT王子 更新时间:2023-10-29 04:22:54 25 4
gpt4 key购买 nike

我正在尝试使用 Entity Framework 5。第一个问题是 EF 会自动创建表。我试图通过包括 dbModelBuilder.Conventions.Remove<PluralizingTableNameConvention>() .第二个问题是这样的错误

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

我尝试通过 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, Entity Framework 5.0.0.0对于 EF 4,它工作完美(没有 OnModelCreating 方法)。

最佳答案

您可以在 OnModelCreating 方法中编写此代码:

modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

关于c# - 如何关闭 Entity Framework 5 的复数表创建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12130059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com