本文介绍了如何关闭Entity Framework 5的复数表创建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use Entity Framework 5. The first problem was that EF creats tables automatically. I tried to fix it by includingdbModelBuilder.Conventions.Remove<PluralizingTableNameConvention>(). The second problem was the error like this

我尝试通过 dbModelBuilder对其进行修复。 Conventions.Remove< IncludeMetadataConvention>(); ,但没有任何意义。
下一个数据访问层:

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;
    }
    }

其他信息:VS 2012,框架4.5,实体框架5.0.0.0
对于EF 4,它可以完美工作(没有OnModelCreating方法)。

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

推荐答案

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

You can write this code in OnModelCreating method:

modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

这篇关于如何关闭Entity Framework 5的复数表创建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 15:59