实体框架创建复数表名,但视图需要单数表名? [英] Entity Framework creates a plural table name, but the view expects a singular table name?

查看:23
本文介绍了实体框架创建复数表名,但视图需要单数表名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MySQL .net 连接器 6.4.4.0 和实体框架 4.1,并尝试创建最基本的代码优先实现.

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.

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

我的模型

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

我的家庭控制器

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)

@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>

它在 mySQL 中创建了具有所有正确属性的表 'votes'.

It creates the table 'votes' in mySQL with all the right properties.

然而,它抛出这一行:

@foreach(模型中的变量项)

@foreach (var item in Model)

例外:

表 'mydb.vote' 不存在"

澄清一下,我实际上想要表复数,它似乎正确地创建了表.我希望找出单数/复数差异的原因.来自 microsoft/Plural Sight/scott gu 的教程和视频都没有使用 mysql 处理,所以我不得不想象 .netconnector 可能是罪魁祸首.我还想避免使用 [Table("Votes")] 属性.基本上,我希望尽可能多的开箱即用"解决方案.

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. I would also like to avoid using the [Table("Votes")] attributes. Basically I'm hoping for as much of an 'out of the box' solution as possible.

edit2(一些更相关的代码):当我删除这个...表格无法一起创建.但该视图抛出异常,寻找投票"而不是投票".在 global.asax 内

edit2 (some more relevant code): when i remove this...tables fail to create all together. but the view throws an exception looking for 'votes' not 'vote'. within 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);
    }
}

推荐答案

所以我放弃了尝试按照我认为应该做的方式去做,并一起删除复数形式.我不确定,但我认为问题与 mysql .net 连接器对 EF 的支持有关.这就是我所做的.

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. Here is what I did.

首先,我的 ApplicationStart 方法中有一个错误:

First, there was a bug in my ApplicationStart method:

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

其次,我停止调用原始代码中未列出的 OnModelCreating 基础实现,因为我只是按照 jgauffin 的建议实现了它:

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:

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

第三,我在一些帖子中读到 MySQL .net 连接器不允许 EF 实际上创建数据库,所以我最初创建了空白数据库.连接器 6.4.4+ 似乎不再是这种情况,只要您的连接字符串的用户有能力创建新数据库,如果最初不存在,它会更好地工作.

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. 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.

有一次,我完成了上述所有操作,它似乎奏效了.所以现在我至少可以前进了.希望我们以后能找出复数/单数差异的原因.

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.

这篇关于实体框架创建复数表名,但视图需要单数表名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆