简体   繁体   English

EntityReference最多只能有一个相关对象

[英]EntityReference can have no more than one related object

I have two identical controller actions and two nearly identical views (one just has a different javscript file with it). 我有两个相同的控制器动作和两个几乎相同的视图(一个视图只是具有一个不同的javscript文件)。 One view works just fine, but the other gets hung up on this EF error: 一个视图工作正常,但另一个视图挂在此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.

I understand the error but it doesn't make any sense in the context, especially when one view works and one doesn't. 我理解该错误,但是在上下文中它没有任何意义,尤其是当一种视图有效而另一种视图无效时。 Here is bit from the two views which is causing it to choke (it's nasty but lets ignore that for now...): 这是两个视图的一部分,这使它窒息(这很讨厌,但现在暂时忽略它...):

<% 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>
                        <%} %>

This is the controller action for the details view which causes the exception: 这是导致异常的详细信息视图的控制器操作:

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);
    }

This is the controller action for the one that works, there is an extra call to populate a drop down list from viewdata, but I removing it doesn't seem to affect one view or the other. 这是一个有效的控制器操作,有一个额外的调用从视图数据中填充下拉列表,但是我删除它似乎并不影响一个视图或另一个视图。

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);
    }

I'm rather stumped by this, any insight is appreciated. 我对此感到困惑,任何见解都值得赞赏。

The error doesn't seem to indicate this problem, but I think you should enumerate your queries first and see what happens. 该错误似乎并不表示存在此问题,但我认为您应该首先枚举查询,然后看看会发生什么。

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

and

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

as it is, you are passing an ObjectQuery to your view and which can cause problems with view rendering since the ObjectContext may not be in the state it was in when you made the query. 实际上,您正在将ObjectQuery传递到视图,这可能会导致视图渲染出现问题,因为ObjectContext可能不处于进行查询时的状态。 Also check you model objects and make sure they are not calling the db from within somewhere. 还要检查模型对象,并确保它们没有从某个地方调用数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM