Deleting an entity instance

Deleting an entity instance from the database has the same variety of options as modifying an entity has:

  • Load the entity to delete in memory and call the method DataAccessAdapter.DeleteEntity().
  • Create a new entity instance into memory, set the PK field value(s) and call the method DataAccessAdapter.DeleteEntity(). This avoids fetching the entity into memory first.
  • You can also delete an entity using an entity collection, using the method DataAccessAdapter.DeleteEntityCollection().
  • You can also delete an entity from the persistent storage directly, using the method DataAccessAdapter.DeleteEntitiesDirectly().

To delete an entity without fetching it first: create the new entity object, set the PK field value and call DeleteEntity(). Instead of using a new entity, you can also pass an existing entity object that's already fetched into memory to DeleteEntity().

using(DataAccessAdapter adapter = new DataAccessAdapter())
{
    CustomerEntity customer = new CustomerEntity("CHOPS");
    adapter.DeleteEntity(customer);
}
Using adapter As New DataAccessAdapter(True)
    Dim customer As New CustomerEntity("CHOPS")
    adapter.DeleteEntity(customer)
End Using
Info

Deletes are never recursive. This means that if the delete action of an entity violates a foreign key constraint, an exception is thrown. To prevent the exception, make sure the FK constraint in the database has been set to use CASCADE DELETE.