Delete fails with An object with a temporary EntityKey value cannot be attached to an object context

5 posts, 0 answers
  1. Shawn
    Shawn avatar
    49 posts
    Member since:
    Jul 2011

    Posted 30 Jun 2012 Link to this post

    I'm using Ajax binding, and my Destroy method looks like this:
    [HttpPost]
    public ActionResult AjaxDelete([DataSourceRequest]DataSourceRequest request, tbl_app_Customer customerToDelete) {
    if (customerToDelete != null) {
    db.tbl_app_Customer.Attach(customerToDelete);
                    db.ObjectStateManager.ChangeObjectState(customerToDelete, EntityState.Deleted);
                    db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
    }
    However this code fails with the error:
    An object with a temporary EntityKey value cannot be attached to an object context

    Any idea why this is? What am I doing wrong?
  2. Shawn
    Shawn avatar
    49 posts
    Member since:
    Jul 2011

    Posted 15 Jul 2012 Link to this post

    Please somebody from Telerik help
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Vitali
    Vitali avatar
    2 posts
    Member since:
    Jul 2012

    Posted 19 Jul 2012 Link to this post

    Hey Shawn, I am was wondering how to configure my destroy action so that it accepts the model I want to destroy
  5. Mark
    Mark avatar
    4 posts
    Member since:
    Jul 2012

    Posted 20 Jul 2012 Link to this post

    Hello? Has anyone found a solution to this? How come the Kendo guys are ignoring this? have exactly the same problem and have been at it for days now, yet I am still stuck. I aso get the same error when I attempt t update.  Please someone who has experience with this, share your knowledge with us? Thank you!
  6. Victor
    Victor avatar
    167 posts
    Member since:
    Nov 2010

    Posted 31 Jul 2012 Link to this post

    I suppose you are using entity framwork? We are too, maybe I can help some... We always take the Id as the argument, not the full record. Then get the entity from the DB and then delete it (similair to updates). Since http is state-less I dont think you can assume that the the object in the db corresponds to the object the client sent. So, your code would be something like:

    [HttpPost]
    public ActionResult AjaxDelete([DataSourceRequest]DataSourceRequest request, Guid id) {
            //id could of course be an int or something else
            try {
                var customer = customerObjectSet.Single(c => c.Id == id);
                //or how you usually get your object from the db
                customerObjectSet.Delete(customer);
                db.SaveChanges();
                //Probably return success message or something
                return Json(ModelState.ToDataSourceResult());
            }
            catch {
                throw new SomethingTerribleWentWrongException();
                //Or whatever error handling
            }
    }

    /Victor
Back to Top
UI for ASP.NET MVC is VS 2017 Ready