实体框架在多台计算机上执行不同的操作

时间:2013-06-27 13:51:05

标签: c# sql asp.net-mvc entity-framework razor

我很茫然,所以我来到这里看看大家对此的看法。

背景:使用带有EF 5的jquery自动完成的文本框

问题:实体正在执行内部联接,但语法显示正在执行左联接。 (这两台PC都运行Win 7和最新的框架)

Anomaly:它在我的电脑上以正确的方式(左外连接)工作。但在我的好友PC上,它显示了一个内部联接。两个文件都是二进制等于。 (实际上是从git中拉出来的)

以下是代码:

public JsonResult AutoCompleteName(string term)
        {
            using (var db = new PersonnelContext())
            {
                return this.Json((from r in db.Personnel
                                  join per in db.PersonnelEmployee on r.Id equals per.Personnel_Id
                                  join dep in db.RefDepartment on per.Department equals dep.Department into rfdp
                                  from g in rfdp.DefaultIfEmpty()
                                  where r.First_Name.ToLower().Contains(term.ToLower()) | r.Last_Name.ToLower().Contains(term.ToLower()) | (r.First_Name.ToLower() + " " + r.Last_Name.ToLower()).Contains(term.ToLower())
                                  select new { firstname = r.First_Name, lastname = r.Last_Name, department = g.DeptDesc ?? "None", per.Personnel_Id, per.Pernr }).OrderBy(a => a.firstname).ToArray(), JsonRequestBehavior.AllowGet);
        }
    }

以下是来自PC的intellitrace,其工作正常:(注意我因保密而拿出一些)

FROM ( SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[First_Name] AS [First_Name], 
    [Extent1].[Last_Name] AS [Last_Name], 
    [Extent2].[Personnel_Id] AS [Personnel_Id], 
    [Extent2].[Pernr] AS [Pernr], 
    CASE WHEN ([Extent3].[Dept_Desc] IS NULL) THEN N'None' ELSE [Extent3].[Dept_Desc] END AS [C1]
    FROM   [Core].[Personnel] AS [Extent1]
    INNER JOIN [Core].[Personnel_Employee] AS [Extent2] ON [Extent1].[Id] = [Extent2].[Personnel_Id]
    LEFT OUTER JOIN [Core].[Ref_Department] AS [Extent3] ON [Extent2].[Department] = [Extent3].[Department]
    WHERE (( CAST(CHARINDEX(LOWER(@p__linq__0), LOWER([Extent1].[First_Name])) AS int)) > 0) OR (( CAST(CHARINDEX(LOWER(@p__linq__1), LOWER([Extent1].[Last_Name])) AS int)) > 0) OR (( CAST(CHARINDEX(LOWER(@p__linq__2), LOWER([Extent1].[First_Name]) + N' ' + LOWER([Extent1].[Last_Name])) AS int)) > 0)
)  AS [Project1]
ORDER BY [Project1].[First_Name] ASC"

正如您所看到的,它正在执行左外连接,就像语法所暗示的那样。

以下是来自PC 2的Intellitrace :(完全相同的代码!)

FROM ( SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[First_Name] AS [First_Name], 
    [Extent1].[Last_Name] AS [Last_Name], 
    [Extent2].[Personnel_Id] AS [Personnel_Id], 
    [Extent2].[Pernr] AS [Pernr], 
    CASE WHEN ([Extent3].[Dept_Desc] IS NULL) THEN N'None' ELSE [Extent3].[Dept_Desc] END AS [C1]
    FROM   [Core].[Personnel] AS [Extent1]
    INNER JOIN [Core].[Personnel_Employee] AS [Extent2] ON [Extent1].[Id] = [Extent2].[Personnel_Id]
    INNER JOIN [Core].[Ref_Department] AS [Extent3] ON [Extent2].[Department] = [Extent3].[Department]
    WHERE (( CAST(CHARINDEX(LOWER(@p__linq__0), LOWER([Extent1].[First_Name])) AS int)) > 0) OR (( CAST(CHARINDEX(LOWER(@p__linq__1), LOWER([Extent1].[Last_Name])) AS int)) > 0) OR (( CAST(CHARINDEX(LOWER(@p__linq__2), LOWER([Extent1].[First_Name]) + N' ' + LOWER([Extent1].[Last_Name])) AS int)) > 0)
)  AS [Project1]
ORDER BY [Project1].[First_Name] ASC"

这是另一个奇怪的异常现象: 当部署到QA时,它在两台PC上运行良好!这里没有一致性让我们知道最新情况。

这里有什么想法吗?我不知道发生了什么。我们完全抹去了我们的工作版本,然后再从git中删除了。同样的事情发生了如果没有人有任何想法,我们可能需要使用proc。但我至少想知道最新情况。

**编辑**答案: 我真的不知道为什么会这样,但我得到了它的工作。这是现在的查询:

return this.Json((from r in db.Personnel
                                  join per in db.PersonnelEmployee on r.Id equals per.Personnel_Id
                                  where r.First_Name.ToLower().Contains(term.ToLower()) | r.Last_Name.ToLower().Contains(term.ToLower()) | (r.First_Name.ToLower() + " " + r.Last_Name.ToLower()).Contains(term.ToLower())
                                  join dep in db.RefDepartment
                                  .Where(x => x.DeptDesc != null || x.DeptDesc == null) on per.Department equals dep.Department into rfdp
                                  from g in rfdp.DefaultIfEmpty()
                                  select new { 
                                      firstname = r.First_Name,
                                      lastname = r.Last_Name, 
                                      department = (g == null) ? "None" : g.DeptDesc,
                                      per.Personnel_Id, 
                                      per.Pernr }).ToArray(), JsonRequestBehavior.AllowGet);

我认为这个有效,因为它明确地寻找DeptDesc为null而不是null ....我很高兴它的工作。感谢所有看过这个的人。

2 个答案:

答案 0 :(得分:1)

我能够通过以下修改后的查询修复此问题:

return this.Json((from r in db.Personnel
                                  join per in db.PersonnelEmployee on r.Id equals per.Personnel_Id
                                  where r.First_Name.ToLower().Contains(term.ToLower()) | r.Last_Name.ToLower().Contains(term.ToLower()) | (r.First_Name.ToLower() + " " + r.Last_Name.ToLower()).Contains(term.ToLower())
                                  join dep in db.RefDepartment
                                  .Where(x => x.DeptDesc != null || x.DeptDesc == null) on per.Department equals dep.Department into rfdp
                                  from g in rfdp.DefaultIfEmpty()
                                  select new { 
                                      firstname = r.First_Name,
                                      lastname = r.Last_Name, 
                                      department = (g == null) ? "None" : g.DeptDesc,
                                      per.Personnel_Id, 
                                      per.Pernr }).ToArray(), JsonRequestBehavior.AllowGet);

正如我在编辑中所说:我不是百分之百确定为什么这样做而不是第一次,但我很高兴它做到了!

答案 1 :(得分:0)

这听起来像是EF [{3}}

已经“固定”的问题

他们注意到删除orderby会删除重复...值得测试呃