Error Message:DeleteRecord-Telerik.OpenAccess.Exceptions.InvalidOperationException: The instance is transient

4 posts, 0 answers
  1. S
    S avatar
    10 posts
    Member since:
    Nov 2013

    Posted 08 Sep 2014 Link to this post

    I got the error when I was trying to delete an entity. I checked the entity and I am pretty sure the entity is there. Is there any other reason?

    the entity object 
    Person_id (PK)
    Email (PK)

    Thank You

  2. Kristian Nikolov
    Admin
    Kristian Nikolov avatar
    206 posts

    Posted 10 Sep 2014 Link to this post

    Hello,

    Thank you for contacting us.

    When you are working with an instance of a persistent class that instance must be managed by an instance of your model`s context class (the one which derives from OpenAccessContext). With this in mind the most likely reason for the exception you are experiencing is that you are attempting to delete an instance of a persistent class which is not managed by a context.

    Such situation can occur in case you attempt to delete a newly created or a cloned instance of a persistent type. We recommend checking your code for logic that might result in such situations and using the AttachCopy method to attach non managed persistent objects to a context instance and then using the returned attached copy  to perform the required operation.

    I hope this helps. Should you have further questions feel free to use our forums again.

    Regards,
    Kristian Nikolov
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
  3. DevCraft banner
  4. Waqar
    Waqar avatar
    7 posts
    Member since:
    Apr 2014

    Posted 23 Oct 2014 in reply to Kristian Nikolov Link to this post

    I used the AttachedCopy method as suggested here but got the following error: InvalidOperationException: The instance is transient

    public ActionResult _Delete([DataSourceRequest]DataSourceRequest request, CustomerViewModel cust)
    {
        if (ModelState.IsValid)
        {
            // Create POCO
            var entity = new OMS_CUSTOMER
            {
                OMS_CUSTOMER_ID = cust.CustomerID,
                CUSTOMERNAME = cust.CustomerName
            };
     
            this.dbContext.AttachCopy(entity);
            this.dbContext.Delete(entity);
        }
     
        // Return the removed product. Also return any validation errors.
        return Json(new[] { cust }.ToDataSourceResult(request, ModelState));
    }
  5. Kristian Nikolov
    Admin
    Kristian Nikolov avatar
    206 posts

    Posted 27 Oct 2014 Link to this post

    Hello Waqar,

    Based on the provided code sample, the most likely reason for the behavior you are experiencing is that you are deleting the initial non managed persistent object entity. The AttachCopy method does not actually attach the persistent object which is passed to it as an argument, but rather it creates a copy of it and attaches that copy. For this reason the entity object is not managed by the context and you receive an exception when attempting to perform a delete with that object.

    AttachCopy returns the instance of the persistent object it has attached to the context, you have to use this instance in order to perform CRUD operations. With this taken into account, the sample code would look similar to the following:
    public ActionResult _Delete([DataSourceRequest]DataSourceRequest request, CustomerViewModel cust)
    {
        if (ModelState.IsValid)
        {
            // Create POCO
            var entity = new OMS_CUSTOMER
            {
                OMS_CUSTOMER_ID = cust.CustomerID,
                CUSTOMERNAME = cust.CustomerName
            };
      
            //attach the persistent object
            var attachedEntity = this.dbContext.AttachCopy(entity);
            //use the attached object returned by the AttachCopy method
            //to perform the delete operation
            this.dbContext.Delete(attachedEntity);
        }
      
        // Return the removed product. Also return any validation errors.
        return Json(new[] { cust }.ToDataSourceResult(request, ModelState));
    }

    I hope this helps. Should you have further questions feel free to post at our forums again.

    Regards,
    Kristian Nikolov
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
Back to Top