The main property of a reference field in D8 is still the target id
:
$node->field_code_used_by->target_id = $user_id;
The entity
property is a computed value. If you put an object in this value, drupal will convert it back to the id before saving it to the dababase.