6

我们在单个开发人员机器和一些客户端上遇到问题。单个 Linq 查询 生成两个不同的 SQL 查询。问题实际上是第二个查询有firebird不支持的“OUTER APPLY”语句。我们认为这不是代码问题,而是环境问题,但我会粘贴代码。

林克:

AIds = (from x in context.RISK_T_ASSESS_HIST
    where (x.ID_RISKOBJECT.HasValue && x.F_CREATEDON >= Freq.StartDate && x.F_CREATEDON <= Freq.EndDate)
    group x by x.ID_RISKOBJECT into gr
    let lastCreated = gr.Max(p => p.F_CREATEDON)
    select new
    {
        ObjId = gr.Key
        ,
        LastStatus = gr.Where(p => p.F_CREATEDON == lastCreated && p.ID_RISKOBJECT == gr.Key).Select(p => p.F_STATUS).FirstOrDefault()
    }).Where(x => x.LastStatus == 0 || x.LastStatus == 1).Select(x => x.ObjId.Value).ToArray();

带有外部应用的 SQL

SELECT 
"G"."ID_RISKOBJECT" AS "ID_RISKOBJECT"
FROM   (SELECT 
    "C"."A1" AS "C1", 
    "C"."K1" AS "ID_RISKOBJECT"
    FROM ( SELECT 
        "E"."ID_RISKOBJECT" AS "K1", 
        MAX("E"."F_CREATEDON") AS "A1"
        FROM "RISK_T_ASSESS_HIST" AS "E"
        WHERE (("E"."ID_RISKOBJECT" IS NOT NULL) AND ("E"."F_CREATEDON" >=     @p__linq__0)) AND ("E"."F_CREATEDON" <= @p__linq__1)     
        GROUP BY "E"."ID_RISKOBJECT"
    )  AS "C" ) AS "G"
OUTER APPLY  (SELECT FIRST (1) 
    "I"."F_STATUS" AS "F_STATUS"
    FROM "RISK_T_ASSESS_HIST" AS "I"
    WHERE (((("I"."ID_RISKOBJECT" IS NOT NULL) AND ("I"."F_CREATEDON" >=     @p__linq__0)) AND ("I"."F_CREATEDON" <= @p__linq__1)) AND (("G"."ID_RISKOBJECT" =         "I"."ID_RISKOBJECT") OR (("G"."ID_RISKOBJECT" IS NULL) AND ("I"."ID_RISKOBJECT" IS     NULL)))) AND (("I"."F_CREATEDON" = "G"."C1") AND ("I"."ID_RISKOBJECT" =     "G"."ID_RISKOBJECT")) ) AS "J"    
WHERE (0 = "J"."F_STATUS") OR (1 = "J"."F_STATUS")

工作 SQL

SELECT 
"B"."ID_RISKOBJECT" AS "ID_RISKOBJECT"
 FROM ( SELECT 
        "C"."ID_RISKOBJECT" AS "ID_RISKOBJECT", 
       (SELECT FIRST (1) 
             "I"."F_STATUS" AS "F_STATUS"
              FROM "RISK_T_ASSESS_HIST" AS "I"
         WHERE (((("I"."ID_RISKOBJECT" IS NOT NULL) AND ("I"."F_CREATEDON" >= @p__linq__0)) AND ("I"."F_CREATEDON" <= @p__linq__1)) AND (("C"."ID_RISKOBJECT" = "I"."ID_RISKOBJECT") OR (("C"."ID_RISKOBJECT" IS NULL) AND ("I"."ID_RISKOBJECT" IS NULL)))) AND (("I"."F_CREATEDON" = "C"."C1") AND ("I"."ID_RISKOBJECT" = "C"."ID_RISKOBJECT"))) AS "C1"
   FROM ( SELECT 
         "D"."A1" AS "C1", 
         "D"."K1" AS "ID_RISKOBJECT"
         FROM ( SELECT 
                "F"."ID_RISKOBJECT" AS "K1", 
                MAX("F"."F_CREATEDON") AS "A1"
                FROM "RISK_T_ASSESS_HIST" AS "F"
                WHERE (("F"."ID_RISKOBJECT" IS NOT NULL) AND ("F"."F_CREATEDON" >= @p__linq__0)) AND ("F"."F_CREATEDON" <= @p__linq__1)
                GROUP BY "F"."ID_RISKOBJECT"
         )  AS "D"
   )  AS "C"    
)  AS "B"
      WHERE (0 = "B"."C1") OR (1 = "B"."C1")

为 MSSQL 引擎生成的查询(我们的 APP 中支持两个 db 引擎)

SELECT 
[Project1].[ID_RISKOBJECT] AS [ID_RISKOBJECT]
FROM   (SELECT 
    [GroupBy1].[A1] AS [C1], 
    [GroupBy1].[K1] AS [ID_RISKOBJECT]
    FROM ( SELECT 
        [Extent1].[ID_RISKOBJECT] AS [K1], 
        MAX([Extent1].[F_CREATEDON]) AS [A1]
        FROM [dbo].[RISK_T_ASSESS_HIST] AS [Extent1]
        WHERE ([Extent1].[ID_RISKOBJECT] IS NOT NULL) AND ([Extent1].[F_CREATEDON] >= @p__linq__0) AND ([Extent1].[F_CREATEDON] <= @p__linq__1)
        GROUP BY [Extent1].[ID_RISKOBJECT]
    )  AS [GroupBy1] ) AS [Project1]
 OUTER APPLY  (SELECT TOP (1) 
    [Extent2].[F_STATUS] AS [F_STATUS]
    FROM [dbo].[RISK_T_ASSESS_HIST] AS [Extent2]
    WHERE ([Extent2].[ID_RISKOBJECT] IS NOT NULL) AND ([Extent2].[F_CREATEDON] >=     @p__linq__0) AND ([Extent2].[F_CREATEDON] <= @p__linq__1) AND (([Project1].[ID_RISKOBJECT] = [Extent2].[ID_RISKOBJECT]) OR (([Project1].[ID_RISKOBJECT] IS NULL) AND ([Extent2].    [ID_RISKOBJECT] IS NULL))) AND ([Extent2].[F_CREATEDON] = [Project1].[C1]) AND ([Extent2].[ID_RISKOBJECT] = [Project1].[ID_RISKOBJECT]) ) AS [Limit1]    
WHERE [Limit1].[F_STATUS] IN (0,1)    
4

2 回答 2

1

您是否正在针对 SQL Server 进行开发?

如果您使用的是 .edmx,那么该ProviderManifestToken属性(通常在 XML 的前 10 行中)将指示支持的 SQL 功能集。这取决于您从数据库创建或更新模型时使用的数据库。例如,一个常见问题是针对 SQL 2008 本地数据库工作,然后推送到 2005 数据库并发现应用程序崩溃,因为它不支持 SQL 2005 中的 datetime2。此实例中的修复方法是将自动创建的值从20082005

我不熟悉对付火鸟,但建议在这个领域寻找。

于 2013-05-20T14:02:42.977 回答
1

问题出在 Net Framework 4.5 和 firebirdsql.data.firebirdclient.dll 之间 - 当某人安装了此版本的框架时 - EntityFramework 为 firebird 生成外部应用语句。

可能 Jiri Cincura (cincura.net) 应该调查它:)

通过将 .net 框架降级到 4.0 full 解决的问题

于 2013-05-21T09:56:04.853 回答