1

我有一个关于 LINQ2SQL 查询生成的 SQL 语句的问题。我有两个数据库表(VisibleForDepartmentId是外键):

AssignableObject                 Department
----------------------           ------------
AssignableObjectId        ┌────> DepartmentId
AssignableObjectType      │
VisibleForDepartmentId ───┘

以及以下映射信息(注意AssignableObject是抽象的):

<Database Name="SO_755661" Class="DataClassesDataContext">
  <Table Name="dbo.AssignableObject" Member="AssignableObjects">
    <Type Name="AssignableObject" Modifier="Abstract">
      <Column Name="AssignableObjectId" Type="System.Int32"
              DbType="Int NOT NULL IDENTITY" IsPrimaryKey="true"
              IsDbGenerated="true" CanBeNull="false" />
      <Column Name="AssignableObjectType" Type="System.String"
              DbType="VarChar(50) NOT NULL" CanBeNull="false"
              AccessModifier="Private" IsDiscriminator="true"/>
      <Column Name="VisibleForDepartmentId" Type="System.Int32"
              DbType="Int" CanBeNull="true" />
      <Association Name="Department_AssignableObject" Member="VisibleForDepartment"
                   ThisKey="VisibleForDepartmentId" OtherKey="DepartmentId"
                   Type="Department" IsForeignKey="true" />
      <Type Name="Asset" InheritanceCode="Asset" IsInheritanceDefault="true" />
      <Type Name="Role" InheritanceCode="Role" />
    </Type>
  </Table>
  <Table Name="dbo.Department" Member="Departments">
    <Type Name="Department">
      <Column Name="DepartmentId" Type="System.Int32"
              DbType="Int NOT NULL IDENTITY" IsPrimaryKey="true"
              IsDbGenerated="true" CanBeNull="false" />
      <Column Name="Name" Type="System.String" DbType="VarChar(50) NOT NULL"
              CanBeNull="false" />
      <Association Name="Department_AssignableObject" Member="AssignableObjects"
                   ThisKey="DepartmentId" OtherKey="VisibleForDepartmentId"
                   Type="AssignableObject" />
    </Type>
  </Table>
</Database>

以及以下代码:

var loadOptions = new DataLoadOptions();
loadOptions.LoadWith<Asset>(a => a.VisibleForDepartment);
dataContext.LoadOptions = loadOptions;
var assets = from a in dataContext.Assets
             select a;

这会产生一个带有两个相同左外连接的 SQL 查询:

SELECT t0.AssignableObjectType, t0.AssignableObjectId, t0.VisibleForDepartmentId,
       t2.test, t2.DepartmentId, t2.Name, t4.test AS test2,
       t4.DepartmentId AS DepartmentId2, t4.Name AS Name2
FROM dbo.AssignableObject AS t0
LEFT OUTER JOIN (
    SELECT 1 AS test, t1.DepartmentId, t1.Name
    FROM dbo.Department AS t1
    ) AS t2 ON t2.DepartmentId = t0.VisibleForDepartmentId
LEFT OUTER JOIN (
    SELECT 1 AS test, t3.DepartmentId, t3.Name
    FROM dbo.Department AS t3
    ) AS t4 ON t4.DepartmentId = t0.VisibleForDepartmentId

为什么有两个外连接,一个就足够了?

亲切的问候,

罗纳德

4

4 回答 4

3

我发现了导致这些重复外连接的原因。它们发生在持久类被两个或多个子类继承时。对于每个子类,如果您使用LoadWith.

在我的示例中,AssignableObject有两个子类:AssetRole. 这导致与Department表的两个外部连接。如果我添加另一个子类,则会添加第三个外连接。

我不确定 SQL Server 是否足够聪明,可以意识到外部连接是重复的。我已经在 Microsoft Connect 上发布了这个。

编辑:显然我的问题是另一个问题的重复,它不会在下一个版本的 LINQ2SQL 中修复。

于 2009-04-22T06:59:41.160 回答
1

您是否不小心在相同 2 个表的相同 2 列之间在数据库上定义了 2 个外键关系?

于 2009-04-16T11:24:30.023 回答
0

您可以尝试在查询本身中进行左外连接。我不确定生成了什么 SQL,因为我这里没有您的数据库。

var assets = from a in dataContext.Assets
             join d in dataContext.Departments on 
                  a.VisibleForDepartmentId equals d.DepartmentId
                  into temp
             from t in temp.DefaultIfEmpty()
             select a;
于 2009-04-17T18:00:15.980 回答
0

我在 LINQPad 中使用其默认数据库创建了一个类似的查询

var loadOptions = new DataLoadOptions();
loadOptions.LoadWith<Products>(a => a.Category);
LoadOptions = loadOptions;

var products = from a in Products
               select a;

products.Dump();

并得到

SELECT [t0].[ProductID], [t0].[ProductName], [t0].[CategoryID], [t2].[test], [t2].[CategoryID] AS [CategoryID2], [t2].[CategoryName]
FROM [Products] AS [t0]
LEFT OUTER JOIN (
    SELECT 1 AS [test], [t1].[CategoryID], [t1].[CategoryName]
    FROM [Categories] AS [t1]
    ) AS [t2] ON [t2].[CategoryID] = [t0].[CategoryID]

正如预期的那样,只使用了一个 OUTER JOIN。

于 2009-04-20T06:34:40.113 回答