![](/img/trans.png)
[英]Entity Framework Code First table naming issue, singular confilcts with Plural
[英]Entity Framework creates a plural table name, but the view expects a singular table name?
我正在使用MySQL .net連接器6.4.4.0和Entity Frame工作4.1,並嘗試創建最基本的代碼優先實現。
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);
}
}
我的強類型視圖(使用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>
它在mySQL中使用所有正確的屬性創建表“ votes”。
但是,它拋出這一行:
@foreach(模型中的var項)
有這個特例:
“表'mydb.vote'不存在”
編輯:澄清一下,我實際上是想要表的復數形式,並且似乎可以正確地創建表。 我希望發現單數/復數差異的原因。 microsoft / Plural Sight / scott gu的教程和視頻都沒有使用mysql處理的,所以我不得不想象.netconnector可能是罪魁禍首。 我也想避免使用[Table(“ Votes”)]屬性。 基本上,我希望有盡可能多的“開箱即用”的解決方案。
edit2(一些更相關的代碼):當我刪除此...表無法一起創建。 但是該視圖引發了一個異常,即尋找“投票”而不是“投票”。 在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);
}
}
因此,我放棄了嘗試以自己認為應該做的方式來做,並一起消除了多元化。 我不確定,但我認為問題與EF的mysql .net連接器有關。 這是我所做的。
首先,我的ApplicationStart方法中存在一個錯誤:
//WRONG
//Database.SetInitializer(new DropCreateDatabaseAlways<myDB>());
Database.SetInitializer(new myDBInitializer());
其次,我不再調用原始代碼中未列出的OnModelCreating基本實現,因為我僅按照jgauffin的建議實現了它:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//DONT DO THIS ANYMORE
//base.OnModelCreating(modelBuilder);
//modelBuilder.Entity<Vote>().ToTable("Votes")
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
第三,我在一些帖子中讀到,MySQL .net Connector不允許EF實際創建數據庫,因此我最初創建了空白數據庫。 連接器6.4.4+似乎不再是這種情況,並且只要您的連接字符串的用戶具有創建新數據庫的能力,如果最初不存在數據庫,它的性能就會更好。
一次,我完成了以上所有操作,它似乎起作用了。 所以現在我至少可以前進了。 希望我們將來能找出造成復數/單數差異的原因。
感謝大家的時間和精力。
在myDB上下文類中,重寫以下方法
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}
使它不對生成的表名進行復數。
打開與要保留單個表名的表相關的DbContext
類。
如果您使用的是EF 6,請添加對以下內容的引用:
using System.Data.Entity.ModelConfiguration.Conventions;
如果是較舊的版本,請添加對以下內容的引用:
using System.Data.Entity.ModelConfiguration.Conventions.Edm.Db;
最后,創建一個重寫OnModelCreating
的方法,並刪除該約定以使表名復數:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
創建實體對象時,刪除“對生成的對象名稱進行復數或單數化”復選標記。
如果您將代碼優先與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.