What I have is this:
- Domain.List has many ListToListMemberships called "SubLists"
- Domain.List also has many ListToListMemberships called "ParentLists"
- ListToListMembership has one List (the parent).
- ListToListMembership has another List (the member).
Given this I want the following test to pass:
[Test]
public void WhenDeletingChildMembershipShouldBeDeletedButNotParent()
{
var list = new Domain.List();
var child = new Domain.List();
var membership = new ListToListMembership()
{
Member = child,
};
list.SubLists.Add(membership);
Session.Save(list);
Session.Flush();
Session.Evict(list);
Session.Evict(child);
var childFromDb = Session.Linq<Domain.List>()
.FirstOrDefault(l => l.ID == child.ID);
Assert.IsNotNull(childFromDb, "child wasn't saved when the parent was saved");
Assert.AreNotSame(child, childFromDb, "child was pulled from cache not the db");
//This will allow it to pass, but I don't want to have to do this.
// foreach (var m in childFromDb.ParentLists)
// {
// m.List.SubLists.Remove(m);
// Session.Save(m.List);
// }
Session.Delete(childFromDb);
Session.Flush();
Session.Evict(childFromDb);
var membershipFromDb = Session.Linq<ListToListMembership>()
.FirstOrDefault(m => m.ID == membership.ID);
Assert.IsNull(membershipFromDb, "membership wasn't deleted");
var parentFromDb = Session.Linq<Domain.List>()
.FirstOrDefault(l => l.ID == list.ID);
Assert.IsNotNull(parentFromDb, "parent list was deleted");
}
Basically when I create a list and add a list membership with a member to it, I want the membership and the member to be created automatically.
Also when I delete either the parent list or the member list, I would like the membership deleted but not the member list (in the case of the parent being deleted) nor the parent list (in the case of the member list being deleted).
Domain.List:
<!-- Other relevant fields -->
[HasMany(typeof(ListToListMembership),ColumnKey="ListId",
Cascade = ManyRelationCascadeEnum.AllDeleteOrphan)]
public IList<ListToListMembership> SubLists{
get;
set;
}
[HasMany(typeof(ListToListMembership),ColumnKey="MemberId",
Cascade = ManyRelationCascadeEnum.AllDeleteOrphan)]
public IList<ListToListMembership> ParentLists {
get;
set;
}
ListToListMembership:
<!-- other relevant fields -->
[BelongsTo("ListId",Cascade=CascadeEnum.SaveUpdate)]
public List List {
get;
set;
}
[BelongsTo("MemberId", Cascade = CascadeEnum.SaveUpdate)]
public List Member {
get;
set;
}