Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
HI
I am new to MVC, I am currently using MVC3 with Entity Framwork.
I am trying to Edit a record in my DB,but cannot due to an error that i cannot for the life of me fix.
Below is erro That is beeing thrown when excuting this lone of code
C#
db.Tickets.Attach(ticket);


Any assistance will be much appreciated. Thank you :)

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

C#
[HttpPost]
        public ActionResult Edit(Ticket ticket)
        {
            if (ModelState.IsValid)
            {

                Ticket ticket1 = db.Tickets.Single(t => t.TicketID == ticket.TicketID);
                ticket.DateCreated = ticket1.DateCreated;
                db.Tickets.Attach(ticket);
                db.ObjectStateManager.ChangeObjectState(ticket, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            //ViewBag.CategoryType_CategoryTypeID = new SelectList(db.CategoryTypes, "CategoryTypeID", "CategoryTypeDesc", ticket.CategoryType_CategoryTypeID);
            ViewBag.HighLevelCategory_HighLevelCategoryID = new SelectList(db.HighLevelCategories, "HighLevelCategoryID", "HighLevelCategoryDesc", ticket.HighLevelCategory_HighLevelCategoryID);
            ViewBag.Priority_PriorityID = new SelectList(db.Priorities, "PriorityID", "PriorityDesc", ticket.Priority_PriorityID);
            ViewBag.ServiceProvider_ServiceProviderID = new SelectList(db.ServiceProviders, "ServiceProviderID", "ServiceProviderName", ticket.ServiceProvider_ServiceProviderID);
            ViewBag.Site_SiteID = new SelectList(db.Sites, "SiteID", "SiteName", ticket.Site_SiteID);
            ViewBag.SubCategory_SubCategoryID = new SelectList(db.SubCategories, "SubCategoryID", "SubCategoryDesc", ticket.SubCategory_SubCategoryID);
            ViewBag.Status_TicketStatusID = new SelectList(db.TicketStatus, "TicketStatusID", "TicketStatusDesc", ticket.Status_TicketStatusID);
            return View(ticket);
        }
Posted

The error means what it says. Before you add an item to the collection, search the collection to make sure it's not there. If you're doing a bulk insert, you must keep track of what you inserted, which is as easy as keeping a list of strings or ints that represents the keys. Don't insert something twice. If you do, you get this error.
 
Share this answer
 
Hi Christian
thanks for the response.

When removing
C#
Ticket ticket1 = db.Tickets.Single(t => t.TicketID == ticket.TicketID);
                ticket.DateCreated = ticket1.DateCreated;


i added date created to the view as a hidden field.
initially it was coming back as null.

HTML
@Html.HiddenFor(model => model.DateCreated)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900