0

I have a table Transactions with 4 Foreign Key references to an Employee table - all nullable INT fields. I create a record in Transactions with 3 of the Employee fields populated. If I then update the remaining null Employee field I get the error:

System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.GetOtherEndOfRelationship(IEntityWrapper wrappedEntity)

I found this SO question which helped me identify the cause, but I still don't understand WHY it happens - Can anyone explain the reason it occurs?

My workaround is to make sure all Employee FK fields are populated on creation of the Transactions.

EDIT - this is not a duplicate- it has NOTHING to do with a simple check for null.

Community
  • 1
  • 1
markpsmith
  • 4,860
  • 2
  • 33
  • 62
  • how do you update the Employee? is your Transaction in the context at this time? when is the exception thrown (I.E. at which line of code) – DevilSuichiro Oct 16 '15 at 08:17
  • I simply get the Transaction from the database, update some properties and call SaveChanges() - nothing unusual at all. If i comment out the line that updates the EmployeeId then it works fine. – markpsmith Oct 16 '15 at 08:43
  • I suspect it is because EF tries to validate Navigation properties. is your Employee entry filled and is a corresponding Empoyee entry already in the database? – DevilSuichiro Oct 16 '15 at 08:45
  • Yes, the Id relates to a valid Employee record. I'm convinced that it's not a data integrity issue, but that it's related to the 4 Employee foreign key constraints. This is code I didn't write and personally I wouldn't have written it like that, but I'd still like to know the answer. – markpsmith Oct 16 '15 at 08:51

0 Answers0