Search code examples
c#nhibernatefluent-nhibernateconventionsautomapping

Fluent NHibernate cascading delete convention for aggregate roots


Question is: how to write convention that prevents deleting entity reference if it inherits from type Root?


I'm using fluent nhibernate and it's auto mapping feature. Turned on cascading delete by default.

Everything was really cool until I finally hit case when I need to actually delete aggregate root that references another one.

I got roots User and Application. User registers Applications. If it's registered by mistake, there is small time gap in which User with role Admin can remove it (I'm kind a sure I won't ever need that data).

When User removes Application, because of cascading delete, nhibernate removes User itself (what an irony...).


I'm unsure how to specify SaveUpdate cascading for Application->User association only:

alt text


Does the trick:

m.References(x=>x.RegisteredBy).Cascade.SaveUpdate(); //RegisteredBy.Type==User

Additionally, told fnh where to look for overrides:

var m=new AutoPersistenceModel(storeCfg);
m.UseOverridesFromAssembly(assembly);

Solution

  • You can override behaviour of cascading for Application->User association. You need to set SaveUpdate() instead of all-delete-orphan. To do this you will need to implement IAutomappingOverride<Application>