How to delete in database without the error
class EObjectAlreadyDetached with message 'Cannot remove or detach object of class TCustomer. The object is not in persistent context.'.In this code I get the error.
aItem: T is converted from rest/json to TCustomer.
But I can't remove it.
procedure TRepositoryDatabase<T>.DeleteItem(aItem: T);The next code works but I must do a load from the database.
begin
inherited;
If assigned(aItem)
then begin
FManager.Remove(aItem); //--> error detach object
FManager.Flush;
end;
end;
That is not a nicesolution.
Is it possible to delete an item (TCustomer) without to call first the database.
procedure TRepositoryDatabase<T>.DeleteItem(aItem: T);
var
oItem: T;
begin
inherited;
oItem:= self.GetItem(aItem.ID);
If assigned(oItem)
then begin
FManager.Remove(oItem);
FManager.Flush;
end;
end;
Regards,
Christophe