diff --git a/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php b/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php
index 5d313a9..a449d4c 100644
--- a/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php
+++ b/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php
@@ -7,12 +7,14 @@
 
 namespace Drupal\dynamic_entity_reference\Plugin\Field\FieldType;
 
+use Drupal\Component\Utility\String;
 use Drupal\Core\Config\Entity\ConfigEntityType;
 use Drupal\Core\Entity\EntityTypeInterface;
 use Drupal\Core\Field\FieldDefinitionInterface;
 use Drupal\Core\Field\FieldItemBase;
 use Drupal\Core\Field\FieldStorageDefinitionInterface;
 use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Session\AccountInterface;
 use Drupal\Core\TypedData\DataDefinition;
 use Drupal\dynamic_entity_reference\DataDynamicReferenceDefinition;
 use Drupal\entity_reference\ConfigurableEntityReferenceItem;
@@ -69,10 +71,14 @@ class DynamicEntityReferenceItem extends ConfigurableEntityReferenceItem {
    * {@inheritdoc}
    */
   public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
-    $properties['target_id'] = DataDefinition::create('string')
-      ->setLabel(t('Entity ID'));
+    $properties['target_id'] = DataDefinition::create('integer')
+      ->setLabel(t('Entity ID'))
+      ->setSetting('unsigned', TRUE)
+      ->setRequired(TRUE);
+
     $properties['target_type'] = DataDefinition::create('string')
       ->setLabel(t('Target Entity Type'));
+
     $properties['entity'] = DataDynamicReferenceDefinition::create('entity')
       ->setLabel(t('Entity'))
       ->setDescription(t('The referenced entity'))
@@ -80,30 +86,18 @@ class DynamicEntityReferenceItem extends ConfigurableEntityReferenceItem {
       ->setComputed(TRUE)
       ->setReadOnly(FALSE);
 
-    if (isset($settings['target_bundle'])) {
-      // @todo Add new NotBundle validator
-      // $properties['entity']->getTargetDefinition()->addConstraint('Bundle', $settings['target_bundle']);
-    }
-
     return $properties;
   }
 
   /**
    * {@inheritdoc}
    */
-  public static function mainPropertyName() {
-    return 'target_id';
-  }
-
-  /**
-   * {@inheritdoc}
-   */
   public static function schema(FieldStorageDefinitionInterface $field_definition) {
     $columns = array(
       'target_id' => array(
         'description' => 'The ID of the target entity.',
-        'type' => 'varchar',
-        'length' => '255',
+        'type' => 'int',
+        'unsigned' => TRUE,
       ),
       'target_type' => array(
         'description' => 'The Entity Type ID of the target entity.',
@@ -115,7 +109,7 @@ class DynamicEntityReferenceItem extends ConfigurableEntityReferenceItem {
     $schema = array(
       'columns' => $columns,
       'indexes' => array(
-        'target_id' => array('target_id'),
+        'target_id' => array('target_id', 'target_type'),
       ),
     );
 
@@ -126,7 +120,7 @@ class DynamicEntityReferenceItem extends ConfigurableEntityReferenceItem {
    * {@inheritdoc}
    */
   public function onChange($property_name, $notify = TRUE) {
-    if ($property_name == 'target_type') {
+    if ($property_name == 'target_type' && !$this->get('entity')->getValue()) {
       $this->get('entity')->getDataDefinition()->getTargetDefinition()->setEntityTypeId($this->get('target_type')->getValue());
     }
     // Make sure that the target type and the target property stay in sync.
@@ -139,6 +133,41 @@ class DynamicEntityReferenceItem extends ConfigurableEntityReferenceItem {
   /**
    * {@inheritdoc}
    */
+  public function getSettableOptions(AccountInterface $account = NULL) {
+    $options = array();
+    $field_definition = $this->getFieldDefinition();
+    $settings = $this->getSettings();
+    $entity_type_ids = static::getAllEntityTypeIds($settings);
+    foreach (array_keys($entity_type_ids) as $entity_type_id) {
+      // We put the dummy value here so selection plugins can work.
+      // @todo Remove these once https://www.drupal.org/node/1959806
+      //   and https://www.drupal.org/node/2107243 are fixed.
+      $field_definition->settings['target_type'] = $entity_type_id;
+      $field_definition->settings['handler'] = $settings[$entity_type_id]['handler'];
+      $field_definition->settings['handler_settings'] = $settings[$entity_type_id]['handler_settings'];
+      $options[$entity_type_id] = \Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($field_definition, $this->getEntity())->getReferenceableEntities();
+    }
+    if (empty($options)) {
+      return array();
+    }
+    $return = array();
+    foreach ($options as $target_type) {
+
+      // Rebuild the array by changing the bundle key into the bundle label.
+      $bundles = \Drupal::entityManager()->getBundleInfo($target_type);
+
+      foreach ($options[$target_type] as $bundle => $entity_ids) {
+        $bundle_label = String::checkPlain($bundles[$bundle]['label']);
+        $return[$entity_type_ids[$target_type]][$bundle_label] = $entity_ids;
+      }
+    }
+
+    return $return;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
   public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
     // @todo inject this.
     $labels = \Drupal::entityManager()->getEntityTypeLabels(TRUE);
@@ -238,7 +267,8 @@ class DynamicEntityReferenceItem extends ConfigurableEntityReferenceItem {
         // its actual id and target_id will be different, due to the new entity
         // marker.
         if (($this->get('entity')->getTargetIdentifier() != $values['target_id']
-            || $this->get('entity')->getTargetDefinition()->getEntityTypeId() != $values['target_type'])) {
+          || $this->get('entity')->getTargetDefinition()->getEntityTypeId() != $values['target_type'])
+          && ($values['target_id'] != static::$NEW_ENTITY_MARKER || !$this->entity->isNew())) {
           throw new \InvalidArgumentException('The target id, target type and entity passed to the dynamic entity reference item do not match.');
         }
       }
@@ -265,40 +295,45 @@ class DynamicEntityReferenceItem extends ConfigurableEntityReferenceItem {
    */
   public function preSave() {
     if ($this->hasNewEntity()) {
-      $this->entity->save();
+      // Handle the case where an unsaved entity was directly set using the
+      // public 'entity' property and then saved before this entity. In this
+      // case the referenced entity will already be saved so no need to save it
+      // again.
+      if ($this->entity->isNew()) {
+        $this->entity->save();
+      }
+      // Make sure the parent knows we are updating this property so it can
+      // react properly.
       $this->target_id = $this->entity->id();
       $this->target_type = $this->entity->getEntityTypeId();
     }
-    // Handle the case where an unsaved entity was directly set using the public
-    // 'entity' property and then saved before this entity. In this case
-    // ::hasNewEntity() will return FALSE but $this->target_id will still be
-    // empty.
-    if ((empty($this->target_id) || empty($this->target_type)) && $this->entity) {
+    elseif ((empty($this->target_id) || empty($this->target_type)) && $this->entity) {
       $this->target_id = $this->entity->id();
       $this->target_type = $this->entity->getEntityTypeId();
     }
   }
 
   /**
-   * Helper function to get all the entity type ids that can be referenced.
-   *
-   * @param array $settings
-   *   The settings of the field storage.
-   *
-   * @return string[]
-   *   All the entity type ids that can be referenced.
+   * {@inheritdoc}
    */
-  public static function getAllEntityTypeIds($settings) {
-    $labels = \Drupal::entityManager()->getEntityTypeLabels(TRUE);
-    $options = $labels['Content'];
-
-    if ($settings['exclude_entity_types']) {
-      $entity_type_ids = array_diff_key($options, $settings['entity_type_ids'] ?: array());
-    }
-    else {
-      $entity_type_ids = array_intersect_key($options, $settings['entity_type_ids'] ?: array());
+  public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
+    $manager = \Drupal::service('plugin.manager.entity_reference.selection');
+    $settings = $field_definition->getSettings();
+    $entity_type_ids = static::getAllEntityTypeIds($settings);
+    foreach (array_keys($entity_type_ids) as $entity_type_id) {
+      $values['target_type'] = $entity_type_id;
+      // We put the dummy value here so selection plugins can work.
+      // @todo Remove these once https://www.drupal.org/node/1959806
+      //   and https://www.drupal.org/node/2107243 are fixed.
+      $field_definition->settings['target_type'] = $entity_type_id;
+      $field_definition->settings['handler'] = $settings[$entity_type_id]['handler'];
+      $field_definition->settings['handler_settings'] = $settings[$entity_type_id]['handler_settings'];
+      if ($referenceable = $manager->getSelectionHandler($field_definition)->getReferenceableEntities()) {
+        $group = array_rand($referenceable);
+        $values['target_id'] = array_rand($referenceable[$group]);
+        return $values;
+      }
     }
-    return $entity_type_ids;
   }
 
   /**
@@ -326,4 +361,26 @@ class DynamicEntityReferenceItem extends ConfigurableEntityReferenceItem {
     return $dependencies;
   }
 
+  /**
+   * Helper function to get all the entity type ids that can be referenced.
+   *
+   * @param array $settings
+   *   The settings of the field storage.
+   *
+   * @return string[]
+   *   All the entity type ids that can be referenced.
+   */
+  public static function getAllEntityTypeIds($settings) {
+    $labels = \Drupal::entityManager()->getEntityTypeLabels(TRUE);
+    $options = $labels['Content'];
+
+    if ($settings['exclude_entity_types']) {
+      $entity_type_ids = array_diff_key($options, $settings['entity_type_ids'] ?: array());
+    }
+    else {
+      $entity_type_ids = array_intersect_key($options, $settings['entity_type_ids'] ?: array());
+    }
+    return $entity_type_ids;
+  }
+
 }
diff --git a/src/Tests/DynamicEntityReferenceItemTest.php b/src/Tests/DynamicEntityReferenceItemTest.php
index 785867d..fa9104b 100644
--- a/src/Tests/DynamicEntityReferenceItemTest.php
+++ b/src/Tests/DynamicEntityReferenceItemTest.php
@@ -188,10 +188,10 @@ class DynamicEntityReferenceItemTest extends FieldUnitTestBase {
     $entity->save();
 
     // Test the generateSampleValue() method.
-//    $entity = EntityTest::create();
-//    $entity->field_der->generateSampleItems();
-//    $entity->field_test_taxonomy_vocabulary->generateSampleItems();
-//    $this->entityValidateAndSave($entity);
+    $entity = EntityTest::create();
+    $entity->field_der->generateSampleItems();
+    $entity->field_der->generateSampleItems();
+    $this->entityValidateAndSave($entity);
   }
 
   /**
