Another possible cause of this is if the foreign key property is set incorrectly.
For example, this can happen in the following scenario:
- DepartmentId is set to zero or any other value that isn't a valid foreign key.
- Department is either null or it is a Department object that contains a null value for its own DepartmentId property.
- This configuration will cause Entity Framework to fail because it tries to find a Department that has a primary key of zero, which probably won't exist.
I ran into this exception once when I mapped one object to another and I incorrectly set the foreign key to zero instead of setting it to null.
I can't say that this configuration is what is causing your exception without seeing the actual values of the properties, but it is one possibility.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…