Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework "An object with the same key already exists in the ObjectStateManager." - I can't understand how, or what to do from here
    primarykey
    data
    text
    <p>I'm building a MVC3 project, using Razor and Entity Framework code first. I have two models:</p> <pre><code>public class Translation { public int TranslationId { get; set; } public string Pt { get; set; } public string Es { get; set; } public string En { get; set; } } public class Page { public int PageId { get; set; } public Translation Title { get; set; } public Translation Description { get; set; } public int? ParentPageId { get; set; } // page can have a parent page public Page ParentPage { get; set; } } </code></pre> <p>I created the PagesController for CRUD for the Page model. Then I edited the Create and Edit views adding inputs for the Translation properties:</p> <pre><code>&lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Title.Pt) @Html.ValidationMessageFor(model =&gt; model.Title.Pt) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Title.Es) @Html.ValidationMessageFor(model =&gt; model.Title.Es) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Title.En) @Html.ValidationMessageFor(model =&gt; model.Title.En) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Description.Pt) @Html.ValidationMessageFor(model =&gt; model.Description.Pt) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Description.Es) @Html.ValidationMessageFor(model =&gt; model.Description.Es) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Description.En) @Html.ValidationMessageFor(model =&gt; model.Description.En) &lt;/div&gt; </code></pre> <p>It works well on Create, adding two new lines to the Translations table (one refered by Title_TranslationId and the other Description_TranslationId) with the input contents. But when Updating, I get the mentioned error at line:</p> <pre><code>db.Entry(page).State = EntityState.Modified; </code></pre> <p>The PagesController is as it was created, so no extra attaches are being made. If I delete one of the Translations inputs at the Edit view, it doesn't throw the error, but doesn't update the translations anyway. </p> <p>The PagesController code:</p> <pre><code>private AdminEntities db = new AdminEntities(); public ViewResult Index() { return View(db.Pages.ToList()); } public ViewResult Details(int id) { Page page = db.Pages.Find(id); return View(page); } public ActionResult Create() { ViewBag.ParentPageId = new SelectList(db.Pages, "ParentPageId", "Description"); return View(); } [HttpPost] public ActionResult Create(Page page) { if (ModelState.IsValid) { db.Pages.Add(page); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.ParentPageId = new SelectList(db.Pages, "ParentPageId", "Description", page.ParentPageId); return View(page); } public ActionResult Edit(int id) { Page page = db.Pages.Find(id); ViewBag.ParentPageId = new SelectList(db.Pages, "ParentPageId", "Description", page.ParentPageId); return View(page); } [HttpPost] public ActionResult Edit(Page page) { if (ModelState.IsValid) { db.Entry(page).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.ParentPageId = new SelectList(db.Pages, "ParentPageId", "Description", page.ParentPageId); return View(page); } } </code></pre> <p>Note that ViewBag.ParentPageId feeds a Pages list that i didn't include in the view code, because it works if I remove the Translation stuff.</p> <p>Any ideas?</p> <p>EDIT: Maybe it is too complicated for EF to handle? Maybe I should change my aproach.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload