Reputation: 12430
I have two objects (many-to-many) Site and Member partially shown as:
public class Site
{
public Site()
{
SiteAuthors = new List<Member>();
}
public virtual IList<Member> SiteAuthors { get; set; }
...
}
public class Member
{
public Member()
{
AuthoredSites = new List<Site>();
}
public virtual IList<Site> AuthoredSites { get; set; }
...
}
I have left the mapping as default, overrides look like this:
public class MemberOverride : IAutoMappingOverride<Member>
{
public void Override(AutoMapping<Member> mapping)
{
mapping.HasManyToMany(x => x.AuthoredSites).Cascade.AllDeleteOrphan();
}
}
public class SiteOverride : IAutoMappingOverride<Site>
{
public void Override(AutoMapping<Site> mapping)
{
}
}
I am having problems deleting sites and members appropriately.
I have tried various combinations of inverse mappings in the overrides, but inevitably one of the cases I need doesn't work.
What I want to achieve is that, when I delete all the members from a site (remember they can still be authors on other sites) - then I want the site to be removed.
When I delete a site from an member, then the site should only be remove if there are no other authors for it.
What is the correct override or mapping configuration to use?
Thanks for your help.
Upvotes: 1
Views: 164
Reputation: 2023
As @Damien_The_Unbeliever has said, this is not quite how cascades are intended to work. However, you can configure NHibernate to take care of this for you. You can handle events in NHibernate with an Event Listener
and do "something else" as you need to.
I found this Hibernate documentation (what NHibernate was ported from) that lists the possible Event Listener
s : http://anandhansubbiah.com/blog/hibernate-events/
The Delete Event Listener
s are likely what you are looking for. This would allow you to detect objects (in your case Member
and Site
) that are about to be deleted, and do something extra - like check to see if other objects need to be deleted.
Upvotes: 3