在一个 Web 应用程序中,我正在使用它:-
using (MvcApplication1.Models.PlumSoftwareEntities db = new Models.PlumSoftwareEntities())
{
var results = (from p in db.Work_Details
where p.CompanyID == 2
select new { p.ID, p.Dated, p.Employee1.Abbrev, p.Activity.ActivityCode }).ToList();
}
它产生这个SQL:-
SELECT
[Extent1].[ID] AS [ID],
[Extent1].[Dated] AS [Dated],
[Extent2].[Abbrev] AS [Abbrev],
[Extent3].[ActivityCode] AS [ActivityCode]
FROM [dbo].[Work Details] AS [Extent1]
INNER JOIN [dbo].[Employees] AS [Extent2] ON [Extent1].[Employee] = [Extent2].[ID]
INNER JOIN [dbo].[Activity] AS [Extent3] ON [Extent1].[ActivityCodeID] = [Extent3].ActivityCodeID]
WHERE 2 = [Extent1].[CompanyID]
在另一个 Web 应用程序中,我使用与上面完全相同的 linq,除了它产生以下 SQL:-
SELECT
[Extent1].[ID] AS [ID],
[Extent1].[Dated] AS [Dated],
[Extent2].[Abbrev] AS [Abbrev],
[Extent3].[ActivityCode] AS [ActivityCode]
FROM [dbo].[Work Details] AS [Extent1]
INNER JOIN [dbo].[Employees] AS [Extent2] ON [Extent1].[Employee] = [Extent2].[ID]
LEFT OUTER JOIN [dbo].[Activity] AS [Extent3] ON [Extent1].[ActivityCodeID] = [Extent3].ActivityCodeID]
WHERE 2 = [Extent1].[CompanyID]
最后一个导航属性现在是 LEFT OUTER JOIN
什么设置,有这种行为的解释。
连接字符串相同,连接到相同的 SQL 服务器,具有相同的登录名,在两个应用程序中使用相同的 Entity Framework 5,都运行 ASP.NET 4.5
这是一个简单的例子,左外连接对我很重要的原因是我使用导航属性将实体表投影到 DTO 对象上,使用 SQlDependency 缓存这些查询。SQLDependency 要求所有视图都必须使用 INNER JOINS,否则会引发错误。