简体   繁体   English

实体框架会创建一个复数表名,但是视图需要一个单数表名吗?

[英]Entity Framework creates a plural table name, but the view expects a singular table name?

I am using MySQL .net connector 6.4.4.0 and Entity Frame work 4.1 and trying to create the most basic of code-first implementations. 我正在使用MySQL .net连接器6.4.4.0和Entity Frame工作4.1,并尝试创建最基本的代码优先实现。

public class myDB: DbContext
{
    public DbSet<Vote> Votes { get; set; }
}

my model 我的模特

public class Vote
{
    public Guid Id { get; set; }
    public int Value { get; set; }
}

my home controller 我的家庭控制器

public class HomeController : Controller
{
    myDB_db = new myDB();
    public ActionResult Index()
    {
        var model = _db.Votes;
        return View(model);
    }
}

my strongly typed view (using List scaffold) 我的强类型视图(使用List脚手架)

@model IEnumerable<Namespace.Models.Vote>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Value)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Value)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
    }

</table>

It creates the table 'votes' in mySQL with all the right properties. 它在mySQL中使用所有正确的属性创建表“ votes”。

However, it throws at this line: 但是,它抛出这一行:

@foreach (var item in Model) @foreach(模型中的var项)

with the exception: 有这个特例:

"Table 'mydb.vote' doesn't exist" “表'mydb.vote'不存在”

edit: To clarify, I actually want table pluralization, and it seems to properly create the table. 编辑:澄清一下,我实际上是想要表的复数形式,并且似乎可以正确地创建表。 I'm hoping to discover the reason for the singular/plural discrepancy. 我希望发现单数/复数差异的原因。 None of the tutorials and videos from microsoft / Plural Sight / scott gu handle using mysql, so i have to imagine that the .netconnector might be the culprit. microsoft / Plural Sight / scott gu的教程和视频都没有使用mysql处理的,所以我不得不想象.netconnector可能是罪魁祸首。 I would also like to avoid using the [Table("Votes")] attributes. 我也想避免使用[Table(“ Votes”)]属性。 Basically I'm hoping for as much of an 'out of the box' solution as possible. 基本上,我希望有尽可能多的“开箱即用”的解决方案。

edit2 (some more relevant code): when i remove this...tables fail to create all together. edit2(一些更相关的代码):当我删除此...表无法一起创建。 but the view throws an exception looking for 'votes' not 'vote'. 但是该视图引发了一个异常,即寻找“投票”而不是“投票”。 within global.asax 在global.asax中

protected void Application_Start()
{
     Database.SetInitializer(new DropCreateDatabaseAlways<myDB>());

     AreaRegistration.RegisterAllAreas();
     RegisterGlobalFilters(GlobalFilters.Filters);
     RegisterRoutes(RouteTable.Routes);
}

public class myDBInitializer : DropCreateDatabaseAlways<myDB>
{
    protected override void Seed(myDBcontext)
    {
        base.Seed(context);
    }
}

So I gave up on trying to do it the way I felt it should be done and removed pluralization all together. 因此,我放弃了尝试以自己认为应该做的方式来做,并一起消除了多元化。 I don't really know for certain, but I assume the problem has to do with the mysql .net connector's support of EF. 我不确定,但我认为问题与EF的mysql .net连接器有关。 Here is what I did. 这是我所做的。

First, there was a bug in my ApplicationStart method: 首先,我的ApplicationStart方法中存在一个错误:

//WRONG
//Database.SetInitializer(new DropCreateDatabaseAlways<myDB>());
Database.SetInitializer(new myDBInitializer());

Second, I stopped calling the OnModelCreating base implementation which is not listed in the original code since I only implemented it as per jgauffin's suggestion: 其次,我不再调用原始代码中未列出的OnModelCreating基本实现,因为我仅按照jgauffin的建议实现了它:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    //DONT DO THIS ANYMORE
    //base.OnModelCreating(modelBuilder);
    //modelBuilder.Entity<Vote>().ToTable("Votes")
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}

Third, I read in some posts that the MySQL .net Connector doesn't let EF actually CREATE a database, so I had initially created the blank DB. 第三,我在一些帖子中读到,MySQL .net Connector不允许EF实际创建数据库,因此我最初创建了空白数据库。 This seems to no longer be the case with connector 6.4.4+, and as long as your connection string's user has the ability to create new databases, it works better if one is not existing initially. 连接器6.4.4+似乎不再是这种情况,并且只要您的连接字符串的用户具有创建新数据库的能力,如果最初不存在数据库,它的性能就会更好。

Once, I did all of the above, it seemed to work. 一次,我完成了以上所有操作,它似乎起作用了。 So now I can at least move forward. 所以现在我至少可以前进了。 Hopefully we can figure out the cause of the plural / singular discrepancy in the future. 希望我们将来能找出造成复数/单数差异的原因。

Thanks to everyone for their time and effort. 感谢大家的时间和精力。

In your myDB context class, override the following method 在myDB上下文类中,重写以下方法

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    base.OnModelCreating(modelBuilder);
}

to have it not pluralize the generated table names. 使它不对生成的表名进行复数。

Open the DbContext class related to the tables you want to keep a singular table name. 打开与要保留单个表名的表相关的DbContext类。

If you are using EF 6, add a reference to: 如果您使用的是EF 6,请添加对以下内容的引用:

using System.Data.Entity.ModelConfiguration.Conventions;

If it is an older version, add a reference to: 如果是较旧的版本,请添加对以下内容的引用:

using System.Data.Entity.ModelConfiguration.Conventions.Edm.Db;

At last create a method that overrides the OnModelCreating and remove the convention to pluralize table names: 最后,创建一个重写OnModelCreating的方法,并删除该约定以使表名复数:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}

Remove "Pluralize or singularsize generated object names" check-mark when you create your entity object. 创建实体对象时,删除“对生成的对象名称进行复数或单数化”复选标记。

在此处输入图片说明

If you're using Code First with EF 6.1 +, override the OnModelCreating event handler and make the following call(s): 如果您将代码优先与EF 6.1 +一起使用,请覆盖OnModelCreating事件处理程序并进行以下调用:

dmbCloud.Conventions.Remove<PluralizingEntitySetNameConvention>();
dmbCloud.Conventions.Remove<PluralizingTableNameConvention>();

您需要在DbContext中重写OnModelCreating以指定表名称:

modelBuilder.Entity<Vote>().MapSingleType().ToTable("Votes")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM