Improved state checking when adding or removing related entity.