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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12130059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:10:40  来源:igfitidea点击:

How turn off pluralize table creation for Entity Framework 5?

c#entity-framework-5pluralize

提问by Yara

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

我正在尝试使用实体框架 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.

自数据库创建以来,支持“CountryContext”上下文的模型已更改。考虑使用 Code First 迁移来更新数据库。

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

我试过修复它,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;
    }
    }

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

附加信息:VS 2012,框架 4.5,实体框架 5.0.0.0 对于 EF 4,它完美运行(没有 OnModelCreating 方法)。

采纳答案by Majid Hosseini

You can write this code in OnModelCreating method:

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

modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

回答by Ladislav Mrnka

If you don't want EF to create tables and manage consistency between you model and database just use this at the startup of your application:

如果您不希望 EF 创建表并管理模型和数据库之间的一致性,只需在应用程序启动时使用它:

Database.SetInitializer<CountryContext>(null);

回答by John Peters

Using LINQ in EF 6.0:

在 EF 6.0 中使用 LINQ:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    var conventions = new List<PluralizingTableNameConvention>().ToArray();
    modelBuilder.Conventions.Remove(conventions);
}