detach object

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);
begin
  inherited;
  If assigned(aItem)
  then begin
    FManager.Remove(aItem);  //--> error detach object
    FManager.Flush;
  end;
end;

The next code works but I must do a load from the database.
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

The object must be in the Object Manager. Either you load it from database, or add it to the manager using Manager.Update(aItem) before calling Remove.


Thanks, this works