Click here to Skip to main content
12,956,234 members (58,903 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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

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.

        public ActionResult Edit(Ticket ticket)
            if (ModelState.IsValid)
                Ticket ticket1 = db.Tickets.Single(t => t.TicketID == ticket.TicketID);
                ticket.DateCreated = ticket1.DateCreated;
                db.ObjectStateManager.ChangeObjectState(ticket, EntityState.Modified);
                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 23-Aug-12 4:13am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Hi Christian
thanks for the response.

When removing
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.HiddenFor(model => model.DateCreated)

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,869
CHill60 3,550
Maciej Los 3,243
ppolymorphe 2,070
Jochen Arndt 1,973

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 24 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100