简体   繁体   中英

Fluent NHibernate Cascade Delete Error

Similar questions have been asked, but I'm not finding an answer so here goes. I have the following Fluent relationship mapped:

HasMany<UserFilter>(x => x.UserProjectFilters)
            .KeyColumns.Add("UserProfileID")
            .Cascade.All()
            .AsSet()
            .Inverse()
            .Cache.ReadWrite();

When I try to delete a parent (Filter entity) though, the delete doesn't cascade; I'm seeing the exception: "The DELETE statement conflicted with the REFERENCE constraint ...". In NH Profiler, I see that the Delete statement is being generated for the parent, but none is generated for the child. I would expect the delete for any children to be executed prior to the parent. What am I doing wrong?

Here's the UserProfileFilter end of the relationship:

References<Filter>(x => x.Filter)
            .Column("FilterID")
            .LazyLoad()
            .Cascade.SaveUpdate();

Thank you! Andy

我认为您需要Cascade.AllDeleteOrphan()因为您已将关系设置为Inverse()

This ended up being a problem with multiple foreign keys on the child table, and the wrong key was being used in the mapping. I changed the code above to the following and it worked just fine:

HasMany<UserFilter>(x => x.UserProjectFilters)
            .KeyColumns.Add("FilterID")
            .Cascade.All()
            .AsSet()
            .Inverse()
            .Cache.ReadWrite();

Thank you for the help David!

Andy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM