I'm still cutting my teeth on the Entity Framework (EF) and have just worked out why I have been receiving this kind of exception:
A relationship is being added or deleted from an AssociationSet 'FK_Shift_ShiftStatus'. With cardinality constraints, a corresponding 'Safety_Shift' must also be added or deleted.
Here is the code:
shift.Status = context.ShiftStatuses.First(x => x.Id == Status.ShiftStartedId);
EF Model:
Shift * <=> 1 ShiftStatus
Database:
Shift.ShiftStatusId has a Foreign Key Relationship with ShiftStatus.Id
After searching the Internet, I was none the wiser. I thought that maybe there was something theoretical that I was missing. In fact the only mistake I had made was that I didn't load the Shift Status reference before assigning it:
shift.StatusReference.Load();
shift.Status = context.ShiftStatuses.First(x => x.Id == Status.ShiftStartedId);