Try this instead:
itemXrefSet = db.Set<ItemXref>(); foreach (var xref in itemsToRemove) { itemXrefSet.Remove(xref); } db.SaveChanges();
This should remove the cross-reference object from the gerund table, as well as the relationship between the two related objects.
The reason you ran into the error, as you tried to make it, was because EntityFramework thought you just needed to delete the relationships without deleting the related object. When EF does this, it tries to set the foreign key column in the dependent table to NULL. The way around this is to either link the table row to another user by changing the value of the UserId column, or by deleting the table row, since you cannot set the required column value to NULL.
source share