This post was motivated by a conversation with Salvador Gascon (@salvadorgascon). Might even go to the pitfalls section… Anyway, the problem is: how many ways there are to delete an entity in NHibernate? And what are the advantages/disadvantages of each?
First, we have the classic one, using ISession.Delete(), which all NHibernate developers should be familiar with:
Using ISession.Get<T>(), you force an entity – and all of its non-lazy associations and properties – to be loaded from the database. When you delete it, all associations marked for cascade delete will also be deleted.
If you don’t want to load the entity from the database in order to delete it, you can use ISession.Load<T>(), for entities marked as lazy, this will return a proxy instead:
Do note, however, that when Delete is called, the entity will still be loaded, as well as all its non-lazy associations. Flushing is required.
For better performance, we can use Executable HQL:
This will translate to a single plain old SQL DELETE command, so no entity will be loaded into the session. This has a big disadvantage, though: no cascade deletes will occur. This means that you may get exceptions due to foreign key violations. A good thing is that it returns the number of deleted records.
Another way to delete is through an overload of the Delete method:
In this case, we are providing the id in the query string, which is not good, mainly for performance reasons – with NHibernate HQL, SQL injections are much more difficult to do. A better alternative is to use positional parameters:
In this case, together with the id, we also need to supply its type. Does not cascade nor return the number of deleted records.
Or, if you want to delete by Id dynamically, check out my extension method here.
Finally, if you are brave enough, you can check my strongly-typed hackish solution here.