0
votes

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

2
Try taking out the .Cascade.SaveUpdate() in the child's reference back to Filter and see what that does. It may help you to solve your problems if you simplify your relationships. If you look at the SQL is it trying to delete the parent (Filter) first? - Cole W

2 Answers

1
votes

I think you need Cascade.AllDeleteOrphan() as you've set up the relationship as Inverse()

1
votes

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