This question is locked. New answers and comments are not allowed.
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
the entity object
Person_id (PK)
Email (PK)
Thank You
3 Answers, 1 is accepted
0
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
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.
0
Waqar
Top achievements
Rank 1
answered on 23 Oct 2014, 10:32 AM
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));
}
0
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:
I hope this helps. Should you have further questions feel free to post at our forums again.
Regards,
Kristian Nikolov
Telerik
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.