0

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

1
  • 1
    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? Commented Mar 31, 2011 at 17:36

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the quick reply but unfortunately this didn't work. I see an NH exception: "An exception occurred when executing batch queries" and there's no innerexception unfortunately. This is the same exception I've been seeing except with the AllDeleteOrphan, it doesn't have an InnerException on it indicating a foreign key violation.
It seems a strange setup - I don't usually have both ends of a M-to-1 relationship cascading. Have you created the schema yourself, or has NHibernate done it?
We created our schema manually. If I remove the Cascade.SaveOrUpdate on the child collection mapping, I still see the error: "The DELETE statement conflicted with the REFERENCE constraint...". Any other thoughts?
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.