2010-07-23 46 views
0

我有两个相同的控制器动作和两个几乎相同的视图(一个只是与它有不同的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); 
    } 

我非常难以忍受这一点,任何洞察力的赞赏。

+0

这是一个标准的EDMX /生成的类或其他东西? 引入强类型视图并将所有代码填充到视图模型中以将其添加到控制器中可能会帮助您找出错误发生的位置。 – 2010-07-24 05:52:19

+0

您需要查看生成的SQL及其数据库结果。错误表明您的模型或数据库或两者都有问题。 – 2010-07-26 14:02:12

+0

@Hightechrider它是生成的,但在包含视图后进行了修改,我添加了一个将视图链接到数据库表的关系。 @Craig我可以通过在一个视图中设置的结果很好,在另一个,但它是空的,这是奇怪的,因为它是相同的查询。 – 2010-07-26 15:33:17

回答

3

该错误似乎并没有表明这个问题,但我认为你应该首先枚举你的查询,看看会发生什么。

var results = (from g in db.BoothGroups 
       where g.PlanID == id 
       select g).ToList(); 

var booths = (from b in db.Booths 
      where b.PlanID == id 
      select b).ToList(); 

,因为它是要传递一个ObjectQuery到您的视图,并且可能会导致视图渲染问题,因为ObjectContext可能不是有人在的时候,你就状态查询。同时检查你的模型对象,并确保它们不在某处调用数据库。

+0

在Linq查询中绝对使用ToList(),并确实将您的视图强类型为System.Web.Mvc.ViewPage <列表>(示例)。 – mare 2010-07-25 16:05:45

相关问题