diff --git a/core/lib/Drupal/Core/Entity/DatabaseStorageController.php b/core/lib/Drupal/Core/Entity/DatabaseStorageController.php
index 5eeb637..2472e11 100644
--- a/core/lib/Drupal/Core/Entity/DatabaseStorageController.php
+++ b/core/lib/Drupal/Core/Entity/DatabaseStorageController.php
@@ -11,9 +11,11 @@
 use PDO;
 use Drupal\Core\Cache\CacheBackendInterface;
 use Drupal\Core\Entity\Query\QueryInterface;
+use Drupal\Core\Entity\Query\Sql\Tables;
 use Drupal\Component\Uuid\Uuid;
 use Drupal\Component\Utility\NestedArray;
 use Drupal\Core\Database\Connection;
+use Drupal\Core\Database\Query\SelectInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
@@ -566,4 +568,11 @@ public function baseFieldDefinitions() {
   public function getQueryServiceName() {
     return 'entity.query.sql';
   }
+
+  /**
+   * Gets the appropriate tables class for the entity type.
+   */
+  public function getTables(SelectInterface $sql_query) {
+    return new Tables($sql_query);
+  }
 }
diff --git a/core/lib/Drupal/Core/Entity/Query/Sql/Condition.php b/core/lib/Drupal/Core/Entity/Query/Sql/Condition.php
index 7323586..1f98472 100644
--- a/core/lib/Drupal/Core/Entity/Query/Sql/Condition.php
+++ b/core/lib/Drupal/Core/Entity/Query/Sql/Condition.php
@@ -24,7 +24,8 @@ public function compile($conditionContainer) {
     // can join tables as necessary. On the other hand, conditions need to be
     // added to the $conditionContainer object to keep grouping.
     $sqlQuery = $conditionContainer instanceof SelectInterface ? $conditionContainer : $conditionContainer->sqlQuery;
-    $tables = new Tables($sqlQuery);
+    $entity_type = $sqlQuery->getMetaData('entity_type');
+    $tables = \Drupal::entityManager()->getStorageController($entity_type)->getTables($sqlQuery);
     foreach ($this->conditions as $condition) {
       if ($condition['field'] instanceOf ConditionInterface) {
         $sqlCondition = new SqlCondition($condition['field']->getConjunction());
diff --git a/core/lib/Drupal/Core/Entity/Query/Sql/ConditionAggregate.php b/core/lib/Drupal/Core/Entity/Query/Sql/ConditionAggregate.php
index cd83691..69b49e8 100644
--- a/core/lib/Drupal/Core/Entity/Query/Sql/ConditionAggregate.php
+++ b/core/lib/Drupal/Core/Entity/Query/Sql/ConditionAggregate.php
@@ -26,7 +26,8 @@ public function compile($conditionContainer) {
     // can join tables as necessary. On the other hand, conditions need to be
     // added to the $conditionContainer object to keep grouping.
     $sql_query = ($conditionContainer instanceof SelectInterface) ? $conditionContainer : $conditionContainer->sqlQuery;
-    $tables = new Tables($sql_query);
+    $entity_type = $sql_query->getMetaData('entity_type');
+    $tables = \Drupal::entityManager()->getStorageController($entity_type)->getTables($sql_query);
     foreach ($this->conditions as $condition) {
       if ($condition['field'] instanceOf ConditionAggregateInterface) {
         $sql_condition = new Condition($condition['field']->getConjunction());
diff --git a/core/lib/Drupal/Core/Entity/Query/Sql/Query.php b/core/lib/Drupal/Core/Entity/Query/Sql/Query.php
index 6fbae92..ff50814 100644
--- a/core/lib/Drupal/Core/Entity/Query/Sql/Query.php
+++ b/core/lib/Drupal/Core/Entity/Query/Sql/Query.php
@@ -289,7 +289,7 @@ protected function result() {
    */
   protected function getSqlField($field, $langcode) {
     if (!isset($this->tables)) {
-      $this->tables = new Tables($this->sqlQuery);
+      $this->tables = $this->entityManager->getStorageController($this->entityType)->getTables($this->sqlQuery);
     }
     $base_property = "base_table.$field";
     if (isset($this->sqlFields[$base_property])) {
