实体框架创建一个复数的表名,但认为预计奇异的表名? [英] Entity Framework creates a plural table name, but the view expects a singular table name?

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

问题描述

我使用的是MySQL .NET连接器6.4.4.0和实体框架工作4.1,并试图创建最基本code-第一的实现。

 公共类MYDB:的DbContext
{
    公共DbSet<表决结果>投票{搞定;组; }
}

我的模型

 公共类投票
{
    公众的Guid标识{搞定;组; }
    公共int值{搞定;组; }
}

我家的控制器

 公共类HomeController的:控制器
{
    myDB_db =新MYDB();
    公众的ActionResult指数()
    {
        VAR模型= _db.Votes;
        返回查看(模型);
    }
}

我的强类型的视图(使用列表支架)

  @model IEnumerable的< Namespace.Models.Vote>@ {
    ViewBag.Title =指数;
}< H2>指数< / H>&所述p为H.;
    @ Html.ActionLink(新建,创建)
&所述; / P>
<表>
    &所述; TR>
        <第i个
            @ Html.DisplayNameFor(型号=> model.Value)
        < /第i
        百分位>< /第i
    < / TR>    @foreach(以型号VAR项){
    &所述; TR>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.Value)
        < / TD>
        &所述; TD>
            @ Html.ActionLink(编辑,编辑,新{ID = item.Id})|
            @ Html.ActionLink(详细信息,详细信息,新{ID = item.Id})|
            @ Html.ActionLink(删除,删除,新{ID = item.Id})
        < / TD>
    < / TR>
    }< /表>

它与所有的右键属性创建在MySQL中表'票'。

但是,在抛出这一行:

@foreach(以型号VAR项目)

与异常:

表'mydb.vote不存在

编辑:
为了澄清,其实我是想表多元化,和它似乎正确地创建表。我希望能找到为单数/复数差异的原因。从微软/复数景观/斯科特谷的教程和视频的处理没有用mysql,所以我必须想象.netconnector可能是罪魁祸首。我也想避免使用[表(投票)]属性。基本上我希望尽可能多的开箱即用的可能的解决方案。

EDIT2(更多相关code):
当我删除此...表未能创建所有一起。
但视图将引发异常寻找'票'不'票'。
在Global.asax中

 保护无效的Application_Start()
{
     Database.SetInitializer(新DropCreateDatabaseAlways&所述; MYDB>());     AreaRegistration.RegisterAllAreas();
     RegisterGlobalFilters(GlobalFilters.Filters);
     的RegisterRoutes(RouteTable.Routes);
}公共类myDBInitializer:DropCreateDatabaseAlways<&MYDB GT;
{
    保护覆盖无效的种子(myDBcontext)
    {
        base.Seed(上下文);
    }
}


解决方案

于是我放弃了在试图做到这一点,我觉得它应该做的方式,去除多元化在一起。我真的不知道肯定,但我认为问题与MySQL的.NET连接器的支持EF要做。下面是我做的。

首先,在我ApplicationStart方法BUG:

  // WRONG
//Database.SetInitializer(new DropCreateDatabaseAlways<&MYDB GT;());
Database.SetInitializer(新myDBInitializer());

第二,我不再叫这是不是在原来的code中,因为我只实现了它按照jgauffin的建议的OnModelCreating基实现:

 保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{
    //不这样做了
    //base.OnModelCreating(modelBuilder);
    //modelBuilder.Entity<Vote>().ToTable(\"Votes)
    modelBuilder.Conventions.Remove&LT; PluralizingTableNameConvention&GT;();
}

第三,我在一些职位读取MySQL的.NET连接器不让EF实际创建一个数据库,所以我最初创建的空白数据库。这似乎不再与连接器6.4.4+的情况下,只要你的连接字符串的用户具有创建新数据库的能力,因此如果一个最初不存在效果更好。

有一次,我做了所有上述情况,它似乎工作。所以,现在我至少可以向前移动。希望我们可以计算出多元/奇异的差异的原因在未来。

感谢大家的时间和精力。

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

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)

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

However, it throws at this line:

@foreach (var item in Model)

with the exception:

"Table 'mydb.vote' doesn't exist"

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

解决方案

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.

First, there was a bug in my ApplicationStart method:

//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:

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. 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天全站免登陆