I have a one-to-one mapping which I'm trying to express with Fluent NHibernate mappings. I know there are a LOT of similar questions/answers on the web but after hours I'm struggling to find an answer to my specific question.
I have a Member and Subscription object.
A member can have 0..1 Subscriptions and a subscription instance can only ever pertain to a single member.
Here are my classes:
public class Member
{
public Subscription Subscription { get; set; }
}
public class Subscription
{
public Member Member { get; set; }
public DateTime StartDate { get; set; }
public DateTime FinishDate { get; set; }
}
Here are my mappings (fluent):
For Member:
mapping.HasOne(member => member.Subscription)
.PropertyRef(subscription => subscription.Member).Cascade.SaveOrUpdate();
For Subscription:
mapping.References(subscription => subscription.Member)
.Unique().Cascade.None();
There are a few requirements but I can't seem to fulfill them all:
- That saving a
Membercascades toSubscription. - Deleting a
Subscriptionbreaks the association on theMember. - Deleting a
Subscriptiondoesn't delete theMember.
If I set Cascade on Member the Subscription is saved, but deleting a Subscription throws an ObjectDeletedException.
Why can't NHibernate automatically remove the association to Subscription first, before applying the cascade on Member?