代码之家  ›  专栏  ›  技术社区  ›  Graham Conzett

EntityReference不能有多个相关对象

  •  0
  • Graham Conzett  · 技术社区  · 15 年前

    我有两个相同的控制器操作和两个几乎相同的视图(其中一个视图有一个不同的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);
        }
    

    我对此感到很困惑,任何见解都会受到赞赏。

    1 回复  |  直到 15 年前
        1
  •  3
  •   Mitch Rosenburg    15 年前

    这个错误似乎并没有指出这个问题,但我认为您应该先枚举您的查询,然后看看会发生什么。

    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 可能未处于进行查询时的状态。还要检查您的模型对象,确保它们不会从某个地方调用db。