1
votes

Ok, I'm working on a project and was just handed a bug that I'm having a bit of trouble with. The code is written in a "different" manner and I think the way the original developers approached this project set it up for some problems, one of which I'm dealing with today. Basically, we have something like this:

Review_Comment comment = commentContext.Review_Comment.First(c => c.CommentID == commentID);
commentContext.DeleteObject(comment);
commentContext.SaveChanges();

review.Review_Comment.Clear();
review.Review_Comment.Load(System.Data.Objects.MergeOption.OverwriteChanges);
context.SaveChanges();

Let me explain a few things and then I'll explain the problem:

  • "review" is an instance of the Review class, which is the parent of a set of "Review_Comments" (i.e. Review_Comments belong to a single Review).
  • The function above is to delete a comment.
  • The comments, for better or worse, use their own EF4 context (separate from the context that the "review" variable is attached to. This is important.
  • What the original developer tried to do, I think was load the comment in a separate context, delete it, then update the EntityCollection of Review_Comments in the separate "Review" class manually.

However, when context.SaveChanges() is called, we get the following error:

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

I've seen this error described when people are trying to delete say, an Order object and the related OrderItems are not deleted correctly but this case is different. We're trying to delete a single child object and then update the EntityCollection on another entity using a separate context.

Hope that all makes sense, let me know if I can help clarify anything. Any thoughts?

EDIT: I should mention I was able to get this problem to go away by using the same context that the rest of the page uses. However, in this case, because of several dependencies previous developers have introduced, I have to keep the second context or else I have to rewrite a ton of code to remove the dependencies on the second context just to fix this bug. I'm hoping to find a solution that doesn't involve that much time. The goal is to delete the comment, then to reload a separate entity's Review_Comment EntityCollection and be able to call SaveChanges() without this error.

1
"Any thoughts?" - don't use a second context?Mitch Wheat
Yeah I have already fixed it by doing that, however it doesn't fly. There's a whole tangled web of stuff relying on the second context. My preference would be to sort all of that out but the priority is on getting the bug fixed. I'd prefer to do it without having to rewrite everything the previous developers did. At this point I'm just looking to fix the immediate problem.Scott

1 Answers

1
votes

Your problem is that the .Clear() causes the second context to disassociate the Review_Comments from its Review, it never realizes that the Review_Comment was actually deleted.

You should do this instead

  context.Refresh(RefreshMode.StoreWins, review.Review_Comment );
  context.SaveChanges();

If you watch the entity state of the comment in "review.Review_Comment" you should see that after the Refresh, its state becomes "Detached" rather than "Modified"