我有两个相同的控制器动作和两个几乎相同的视图(一个只是与它有不同的javscript文件)。一种观点工作得很好,但其他被挂了此EF错误:EntityReference只能有一个相关的对象
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);
}
我非常难以忍受这一点,任何洞察力的赞赏。
这是一个标准的EDMX /生成的类或其他东西? 引入强类型视图并将所有代码填充到视图模型中以将其添加到控制器中可能会帮助您找出错误发生的位置。 – 2010-07-24 05:52:19
您需要查看生成的SQL及其数据库结果。错误表明您的模型或数据库或两者都有问题。 – 2010-07-26 14:02:12
@Hightechrider它是生成的,但在包含视图后进行了修改,我添加了一个将视图链接到数据库表的关系。 @Craig我可以通过在一个视图中设置的结果很好,在另一个,但它是空的,这是奇怪的,因为它是相同的查询。 – 2010-07-26 15:33:17