简体   繁体   中英

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). One view works just fine, but the other gets hung up on this EF error:

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. Also check you model objects and make sure they are not calling the db from within somewhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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