_NT
_NT

Reputation:

NHibernate- Prevent deletion on a particular entity (i.e. make read-only)

How can I prevent NHibernate from deleting a single entity of a specific class? A programmatic way I am using at the moment entails checking for the entity's unique field "Name".

Here's the scenario: I have a person and a group. The group can have persons and other groups. If the group named "Admins" is attempted to be deleted, it will not, due to transaction-level constraints that I enforce (specifically checking for the group's 'Name' column/field, as this is unique). So that's fine.

But now I'm thinking that if another group is created and the "Admins" becomes a sub-group of that, the check will fail. This will mean the deletion of "Admins". So I'm looking for a better way, other than traversing the parent/child containment tree, e.g. using NHibernate

I can't use a class-wide restriction such as 'class Mutable=false', I mean having a read-only restriction on one or two individual entities of a certain class.

Regards,

_NT

Upvotes: 4

Views: 1124

Answers (1)

Canton
Canton

Reputation: 807

You can write your own implementation of IPreDeleteEventListener and hook into nhibernate's event system to programmatically stop and entity being deleted.

This is an example of using listeners.

Upvotes: 3

Related Questions