开发者

cascade delete multiple levels with nhibernate?

I have a hierarchy of tables 3 levels deep (QualificaionType has many QualificationGroups, which have many Qualifications) mapped like this:

// QualificationType
HasMany(x => x.QualificationGroups)
    .Inverse()
    .KeyColumns.Add("QualificationGroupId")
    .AsBag()
    .Cascade.AllDeleteOrphan()
    .Access.CamelCaseField(Prefix.Underscore);

// Qualification Group
HasMany(x => x.Qualifications)
    .Inverse()
    .KeyColumns.Add("QualificationId")
    .AsBag()
    .Cascade.AllDeleteOrphan()
    .Access.CamelCaseField(Prefix.Underscore);

When I delete a Qualification Group, it deletes all qualifications related to it. But when I try to delete a Qualification Type, it tries to delete all related Qualification Groups but not their Qualifications.

How can I get it to cascade all the way down so that if I delete a Type, it deletes all the Groups and all of their Qualifications? Do I need to loop through all the Groups first and delete them? It seems this should be able to be handled开发者_JAVA技巧 through mappings alone.

FYI, I'm using Fluent NHibernate 1.0RTM and NHibernate 2.1


I figured it out... mappings were incorrect.

They should be this:

// QualificationType
HasMany(x => x.QualificationGroups)
    .Inverse()
    .KeyColumn("QualificationTypeId")
    .AsBag()
    .Cascade.AllDeleteOrphan()
    .Access.CamelCaseField(Prefix.Underscore);

// Qualification Group
HasMany(x => x.Qualifications)
    .Inverse()
    .KeyColumn("QualificationGroupId")
    .AsBag()
    .Cascade.AllDeleteOrphan()
    .Access.CamelCaseField(Prefix.Underscore);

I had the wrong key column in each.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜