diff --git a/entityreference.module b/entityreference.module
index bdcb562..a05a55c 100644
--- a/entityreference.module
+++ b/entityreference.module
@@ -672,6 +672,11 @@ function entityreference_field_property_callback(&$info, $entity_type, $field, $
   // Then apply the default.
   entity_metadata_field_default_property_callback($info, $entity_type, $field, $instance, $field_type);
 
+  // But use our property setter.
+  $name = $field['field_name'];
+  $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$name];
+  $property['setter callback'] = 'entityreference_property_setter';
+
   // Invoke the behaviors to allow them to change the properties.
   foreach (entityreference_get_behavior_handlers($field, $instance) as $handler) {
     $handler->property_info_alter($info, $entity_type, $field, $instance, $field_type);
@@ -679,6 +684,44 @@ function entityreference_field_property_callback(&$info, $entity_type, $field, $
 }
 
 /**
+ * Custom entity metadata property setter for entityreference.
+ *
+ * Here we deal with cases where entity records a name or uuid as the identifier
+ * of a target entity.  We need to store the numeric id, so we try to find it out
+ * here.
+ */
+function entityreference_property_setter($entity, $name, $value, $langcode, $entity_type) {
+  // We only need to process non-numeric ids.
+  if (!is_numeric($value)) {
+    $target_id = FALSE;
+    // Discover the entity type of the target entity, and get its info.
+    $field = field_info_field($name);
+    $target_type = $field['settings']['target_type'];
+    $target_info = entity_get_info($target_type);
+    // If this entity type supports a 'name' key, then try loading the entity
+    // by name in order to extract its numeric id.
+    if (isset($target_info['entity keys']['name'])) {
+      if ($target_entity = entity_load_single($target_type, $value)) {
+        list($target_id) = entity_extract_ids($target_type, $target_entity);
+      }
+    }
+    // @todo Do something similar to support UUID.
+    // @see https://drupal.org/node/2123779
+    //if (!$target_id && module_exists('uuid') && isset($target_info['entity keys']['uuid'])) {
+    //  $ids = entity_get_id_by_uuid($target_type, array($value));
+    //  if (!empty($ids)) {
+    //    $target_id = reset($ids);
+    //  }
+    //}
+    // Replace the value with the numeric target id, if we found one.
+    if ($target_id) {
+      $value = $target_id;
+    }
+  }
+  entity_metadata_field_property_set($entity, $name, $value, $langcode, $entity_type);
+}
+
+/**
  * Implements hook_field_widget_info().
  */
 function entityreference_field_widget_info() {
