diff --git a/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php b/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php
index ae014e8..9261522 100644
--- a/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php
+++ b/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php
@@ -200,4 +200,21 @@ class DynamicEntityReferenceItem extends ConfigurableEntityReferenceItem {
     return $this->values;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function preSave() {
+    if ($this->hasUnsavedEntity()) {
+      $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
+    // ::hasUnsavedEntity() will return FALSE but $this->target_id and
+    // $this->target_type will still be empty.
+    if (empty($this->target_id) && $this->entity) {
+      $this->target_type = $this->entity->getEntityTypeId();
+      $this->target_id = $this->entity->id();
+    }
+  }
+
 }
diff --git a/src/Tests/DynamicEntityReferenceItemTest.php b/src/Tests/DynamicEntityReferenceItemTest.php
new file mode 100644
index 0000000..bacb0c5
--- /dev/null
+++ b/src/Tests/DynamicEntityReferenceItemTest.php
@@ -0,0 +1,167 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\dynamic_entity_reference\Tests\EntityReferenceItemTest.
+ */
+
+namespace Drupal\dynamic_entity_reference\Tests;
+
+use Drupal\Core\Field\FieldItemListInterface;
+use Drupal\Core\Field\FieldItemInterface;
+use Drupal\Core\Language\LanguageInterface;
+use Drupal\field\Tests\FieldUnitTestBase;
+use Drupal\taxonomy\Entity\Term;
+use Drupal\Component\Utility\Unicode;
+
+/**
+ * Tests the new entity API for the entity reference field type.
+ *
+ * @group dynamic_entity_reference
+ */
+class DynamicEntityReferenceItemTest extends FieldUnitTestBase {
+
+  /**
+   * Modules to install.
+   *
+   * @var array
+   */
+  public static $modules = array('dynamic_entity_reference', 'taxonomy', 'text', 'filter');
+
+  /**
+   * The taxonomy vocabulary to test with.
+   *
+   * @var \Drupal\taxonomy\VocabularyInterface
+   */
+  protected $vocabulary;
+
+  /**
+   * The taxonomy term to test with.
+   *
+   * @var \Drupal\taxonomy\TermInterface
+   */
+  protected $term;
+
+  /**
+   * Sets up the test.
+   */
+  protected function setUp() {
+    parent::setUp();
+
+    $this->installEntitySchema('taxonomy_term');
+
+    $this->vocabulary = entity_create('taxonomy_vocabulary', array(
+      'name' => $this->randomMachineName(),
+      'vid' => Unicode::strtolower($this->randomMachineName()),
+      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
+    ));
+    $this->vocabulary->save();
+
+    $this->term = entity_create('taxonomy_term', array(
+      'name' => $this->randomMachineName(),
+      'vid' => $this->vocabulary->id(),
+      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
+    ));
+    $this->term->save();
+
+    // Use the util to create an instance.
+    entity_create('field_storage_config', array(
+      'field_name' => 'field_test_taxonomy_term',
+      'type' => 'dynamic_entity_reference',
+      'entity_type' => 'entity_test',
+      'cardinality' => 1,
+      'settings' => array(
+        'exclude_entity_types' => FALSE,
+        'entity_type_ids' => array(
+          'taxonomy_term' => 'taxonomy_term',
+        ),
+      ),
+    ))->save();
+
+    entity_create('field_config', array(
+      'field_name' => 'field_test_taxonomy_term',
+      'entity_type' => 'entity_test',
+      'bundle' => 'entity_test',
+      'label' => 'Foo Bar',
+      'settings' => array(),
+    ))->save();
+  }
+
+  /**
+   * Tests the entity reference field type for referencing content entities.
+   */
+  public function testContentEntityReferenceItem() {
+    $tid = $this->term->id();
+    $entity_type_id = $this->term->getEntityTypeId();
+    // Just being able to create the entity like this verifies a lot of code.
+    $entity = entity_create('entity_test');
+    $entity->field_test_taxonomy_term->target_id = $tid;
+    $entity->field_test_taxonomy_term->target_type = $entity_type_id;
+    $entity->name->value = $this->randomMachineName();
+    $entity->save();
+
+    $entity = entity_load('entity_test', $entity->id());
+    $this->assertTrue($entity->field_test_taxonomy_term instanceof FieldItemListInterface, 'Field implements interface.');
+    $this->assertTrue($entity->field_test_taxonomy_term[0] instanceof FieldItemInterface, 'Field item implements interface.');
+    $this->assertEqual($entity->field_test_taxonomy_term->target_id, $tid);
+    $this->assertEqual($entity->field_test_taxonomy_term->target_type, $entity_type_id);
+    $this->assertEqual($entity->field_test_taxonomy_term->entity->getName(), $this->term->getName());
+    $this->assertEqual($entity->field_test_taxonomy_term->entity->id(), $tid);
+    $this->assertEqual($entity->field_test_taxonomy_term->entity->uuid(), $this->term->uuid());
+
+    // Change the name of the term via the reference.
+    $new_name = $this->randomMachineName();
+    $entity->field_test_taxonomy_term->entity->setName($new_name);
+    $entity->field_test_taxonomy_term->entity->save();
+    // Verify it is the correct name.
+    $term = Term::load($tid);
+    $this->assertEqual($term->getName(), $new_name);
+
+    // Make sure the computed term reflects updates to the term id.
+    $term2 = entity_create('taxonomy_term', array(
+      'name' => $this->randomMachineName(),
+      'vid' => $this->term->bundle(),
+      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
+    ));
+    $term2->save();
+
+    $entity->field_test_taxonomy_term->target_id = $term2->id();
+    $entity->field_test_taxonomy_term->target_type = $entity_type_id;
+    $this->assertEqual($entity->field_test_taxonomy_term->entity->id(), $term2->id());
+    $this->assertEqual($entity->field_test_taxonomy_term->entity->getName(), $term2->getName());
+
+    // Delete terms so we have nothing to reference and try again
+    $term->delete();
+    $term2->delete();
+    $entity = entity_create('entity_test', array('name' => $this->randomMachineName()));
+    $entity->save();
+
+    // Test the generateSampleValue() method.
+//    $entity = entity_create('entity_test');
+//    $entity->field_test_taxonomy_term->generateSampleItems();
+//    $entity->field_test_taxonomy_vocabulary->generateSampleItems();
+//    $this->entityValidateAndSave($entity);
+  }
+
+  /**
+   * Test saving order sequence doesn't matter.
+   */
+  public function testEntitySaveOrder() {
+    // The term entity is unsaved here.
+    $term = entity_create('taxonomy_term', array(
+      'name' => $this->randomMachineName(),
+      'vid' => $this->term->bundle(),
+      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
+    ));
+    $entity = entity_create('entity_test');
+    // Now assign the unsaved term to the field.
+    $entity->field_test_taxonomy_term->entity = $term;
+    $entity->name->value = $this->randomMachineName();
+    // Now save the term.
+    $term->save();
+    // And then the entity.
+    $entity->save();
+    $this->assertEqual($entity->field_test_taxonomy_term->entity->id(), $term->id());
+  }
+
+}
