Search code examples
nhibernatefluent-nhibernatefluent-nhibernate-mapping

Fluent NHibernate map entity so that it can only perform updates and selects


Im on a legacy system where triggers are abundant in the database.

Therefore I have an entity mapped in Fluent NHB on which I would like to enforce that only updates and selects can be performed, as there is a trigger on another table which performs the inserts into this table.

In short i want to ensure that it iss not possible to do inserts with this entity.

Something along the line .Not.Insert(), but for the entity and not a column.

Note: Triggers are often a bad choice for stuff like this, but due to the complexity i do not have the option to delete the trigger just yet.

Can this be done with Fluent NHB?


Solution

  • I don't think there is a simple ("designed") way to do it, but I can think of the following potential options:

    Custom IEntityPersister (IClassPersister). Derive from the default persister, then override the Insert() methods to throw NotSupportedException. Must be configured in the mapping (not sure if this step can be done through FluentNH): http://nhibernate.info/doc/nh/en/index.html#mapping-declaration-class

    Or implement a suitable NHibernate event listener that can detect INSERT statements for a number of tables, and prevent these by throwing.

    Or make your domain classes have private/protected setters for the Id property, then use the mapping to tell NHibernate that the "assigned" id generator should be used. NHibernate can now not INSERT such objects unless the application sets the Id, which should never happen since it's protected (sort of...).