none
An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key. RRS feed

  • Question

  • I am trying to update multiple records and was unable to find the solution inj any of the postings.  Here is my code:

    [HttpPost]
    public ActionResult UpdateAll(ICollection<Mysurvey> mysurveys)
            {
                           
                for (int i = 0; i < mysurveys.Count; i++)
                {
                    db.Entry(mysurveys.ElementAt(i)).State = EntityState.Modified;
                }
                db.SaveChanges();
                return View(mysurveys);
            }

    I get this error at .Modified line
    Thursday, June 7, 2012 4:55 PM

Answers

  • Hi Allen,

    I got it working by adding to my view the following line:

    <%: Html.HiddenFor(m => m[i].ID)%>
    Thank you for your help

    michael vokhgelt

    • Marked as answer by Michael V OH Friday, June 8, 2012 1:37 PM
    Friday, June 8, 2012 1:37 PM

All replies

  • Hi Michael V OH,

    Welcome to MSDN Forum.

    Could you please clarify what the 'mysurveys' is? Based on the exception, I assume the collection stores all the new elements which have not been persist into database, but some of them have the same key with the records in the database, so when you change the entity state, the exception is thrown.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Friday, June 8, 2012 4:35 AM
    Moderator
  • Allen thanks for the quick reply,

    I am working with MVC application. Mysurvey is the model - here is Mysurvey.cs file

    using System;
    using System.Linq;
    using System.Data.Entity;
    namespace MvcMysurvey.Models
    {
        public class Mysurvey
        {
            public int ID { get; set; }
            public string SectionName { get; set; }
            public string Quarter { get; set; }
            public string Comment { get; set; }
        }
        public class MysurveyDBContext : DbContext
        {
            public DbSet<Mysurvey> Mysurveys { get; set; }
        }
    }

    here is the code in my controller:

            public ActionResult UpdateAll()
           {
               return View(db.Mysurveys.ToList()); 
           }
           //Action method on HomeController
            [HttpPost]
            public ActionResult UpdateAll(ICollection<Mysurvey> mysurveys)
            {
                foreach (var survey in mysurveys)
                {
                    if (ModelState.IsValid)
                    {
                        db.Entry(survey).State = EntityState.Modified;
             
                    }
                  }
                db.SaveChanges();
                return View(mysurveys);
            }

    and finally here is the view

    <% for (int i = 0; i < Model.Count; i++) { %>
                
                <tr>
                     <td>
                        <%: Html.DisplayFor(m => m[i].ID) %>
                    </td>
                   <td>
                        <%: Html.TextBoxFor(m => m[i].SectionName) %>
                        <%: Html.ValidationMessageFor(m => m[i].SectionName) %>
                    </td>
                    <td>
                        <%: Html.TextBoxFor(m => m[i].Quarter) %>
                        <%: Html.ValidationMessageFor(m => m[i].Quarter) %>
                    </td>
                    <td>
                        <%: Html.TextBoxFor(m => m[i].Comment) %> 
                        <%: Html.ValidationMessageFor(m => m[i].Comment) %>
                    </td>
                </tr>
           
    I am brand new to this and have a tight deadline at work for the current project.
    Thank you

    michael vokhgelt

    Friday, June 8, 2012 11:18 AM
  • Hi Allen,

    I pressed the Reply button and then realized that I replied to myself and I am not sure if you will get notification.

    The reply to your question is in a thread above.

    Thank you for your help


    michael vokhgelt

    Friday, June 8, 2012 11:23 AM
  • Hi Allen,

    I got it working by adding to my view the following line:

    <%: Html.HiddenFor(m => m[i].ID)%>
    Thank you for your help

    michael vokhgelt

    • Marked as answer by Michael V OH Friday, June 8, 2012 1:37 PM
    Friday, June 8, 2012 1:37 PM