0

I am getting the error "The relationship could not be changed because one or more of the foreign-key properties is non-nullable" when trying to update an EF object. The object graph is incredibly complicated so I can't post all the code. My question is how can I get the actual field/property or object that is causing this generic error message? I would take me forever to find it by looking around in the code and trying to find the offending object.

  • Did you try debugging and step through the code, do you get line number/section of code at least? What DB technology? – Brad Feb 18 '19 at 20:14
  • https://stackoverflow.com/questions/5538974/the-relationship-could-not-be-changed-because-one-or-more-of-the-foreign-key-pro – Alexan Feb 18 '19 at 20:17
  • I have looked at other answers. They are telling me how to go through objects, remove child items, etc. That is really a last resort. The objects are massive. I'm looking for guidance as to how to find out WHICH property or object is causing the error. – user2011565 Feb 18 '19 at 20:29
  • Place a breakpoint on or prior to the SaveChanges call where you have access to the entity you are updating. Look through the properties for any/all reference properties that are #null and check those against non-nullable FKs. Start an SQL trace if it is getting so far as to try and feed a query to the database and the DB is rejecting it based on a null FK. From the SQL being executed you should see exactly which FKs are either set to null. Looks like you're faced with an elephant of an entity. How do you eat an elephant? Like anything else, one bite at time. – Steve Py Feb 18 '19 at 22:48

0 Answers0