我有两个相同的控制器操作和两个几乎相同的视图(其中一个视图有一个不同的javscript文件)。一个视图工作正常,但另一个视图挂断了此ef错误:
A relationship multiplicity constraint violation occurred: An EntityReference can have no more than one related object, but the query returned more than one related object. This is a non-recoverable error.
我理解这个错误,但在上下文中没有任何意义,尤其是当一个视图工作,而另一个视图不工作时。下面是两个视图中的一点,它导致了阻塞(这很糟糕,但现在让我们忽略它…):
<% var x = ((IEnumerable<Project.Models.Booth>)ViewData["booths"]).Where(b => b.RowNumber == i && b.ColumnNumber == j && b.BoothGroupID == item.ID).FirstOrDefault(); %>
<%if ( x != null)
{ %>
<td class="assigned" title="<%: x.BoothNumber %> - <%: x.Exhibitor.Name %>" style="width:<%: item.Width %>px; height:<%: item.Height %>px;"></td>
<%}
else
{ %>
<td style="width:<%: item.Width %>px; height:<%: item.Height %>px;"></td>
<%} %>
这是导致异常的详细信息视图的控制器操作:
public ActionResult Details(int id)
{
var results = from g in db.BoothGroups
where g.PlanID == id
select g;
ViewData["id"] = id;
var plan = (from p in db.Plans
where p.ID == id
select p).FirstOrDefault();
ViewData["imagePath"] = "/plans/" + plan.Name.ToString().Replace(" ", "").ToLower() + "/" + plan.ImageFileName;
var booths = from b in db.Booths
where b.PlanID == id
select b;
ViewData["booths"] = booths;
return View(results);
}
这是一个有效的控制器操作,有一个额外的调用从viewdata填充下拉列表,但我删除它似乎不会影响一个或另一个视图。
public ActionResult EditAssignment(int id)
{
var results = from g in db.BoothGroups
where g.PlanID == id
select g;
ViewData["id"] = id;
var plan = (from p in db.Plans
where p.ID == id
select p).FirstOrDefault();
ViewData["imagePath"] = "/plans/" + plan.Name.ToString().Replace(" ", "").ToLower() + "/" + plan.ImageFileName;
var exhibitors = from e in db.Exhibitors
where e.MeetingCode == plan.MeetingCode
orderby e.Name
select e;
ViewData["exhibitors"] = new SelectList(exhibitors, "ID", "Name");
var booths = from b in db.Booths
where b.PlanID == id
select b;
ViewData["booths"] = booths;
return View(results);
}
我对此感到很困惑,任何见解都会受到赞赏。