Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# MVC3 Razor
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
db.Tickets.Attach(ticket);
 
Any assistance will be much appreciated. Thank you Smile | :)
 
An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
 
[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 23-Aug-12 4:13am
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
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)
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 24 Aug 2012
Copyright © CodeProject, 1999-2014
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