我可以强制Linq to Sql使用Sql2005Provider吗 [英] Can I force Linq to Sql to use Sql2005Provider

查看:73
本文介绍了我可以强制Linq to Sql使用Sql2005Provider吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此查询(或类似的查询)在我们已经部署的代码库中.

This query (or rather one similar to it) is in a codebase we have already deployed.

var timeblocks = from tb in DB.TimeBlocks
    where tb.StartDate.Date.AddDays(1) <= DateTime.Today
    select tb.Id;

DB是连接到数据库的数据上下文. TimeBlocks是一个非常简单的表,StartDate是一个DateTime列.

DB is a datacontext that connects to the database. TimeBlocks is a fairly simple table, StartDate is a DateTime column.

当前数据库托管在Sql Server 2005安装中,但是我们正在升级到Sql Server 2008安装中.

Currently the database is hosted on a Sql Server 2005 Installation, but we are in the process of upgrading to a Sql Server 2008 Installation.

查询当前可以正常执行.

The query currently exectutes without problems.

如果我们更改连接字符串以指向在Sql Server 2008上运行的同一数据库的副本(其兼容级别设置为2005),则查询将失败,并出现SqlException:

If we change the connection string to point at a copy of the same database running on Sql Server 2008 (with the compatability level set for 2005) the query fails with the SqlException:

数据类型为date的日期函数dateadd不支持datepart毫秒."

"The datepart millisecond is not supported by date function dateadd for data type date."

这似乎是由于Linq连接到2008db时,它向SQL发出的SQL不同(我认为这是因为它使用Sql2008Provider而不是Sql2005Provider).

This seems to be due to the different sql emitted by Linq to SQL when it connects to the 2008db (I assume this is because it uses Sql2008Provider instead of the Sql2005Provider).

2005 Provider产生了良好的SQL:

The 2005 Provider produces good SQL:

SELECT [t0].[Id]
FROM [dbo].[TimeBlock] AS [t0]
WHERE DATEADD(ms, (CONVERT(BigInt,@p0 * 86400000)) % 86400000, DATEADD(day, (CONVERT(BigInt,@p0 * 86400000)) / 
    86400000, DATEADD(HOUR, -DATEPART(HOUR, [t0].[StartDate]), DATEADD(MINUTE, -DATEPART(MINUTE, 
        [t0].[StartDate]), DATEADD(SECOND, -DATEPART(SECOND, [t0].[StartDate]), DATEADD(MILLISECOND,
        -DATEPART(MILLISECOND, [t0].[StartDate]), [t0].[StartDate])))))) <= @p1

成功执行查询.但是,Sql2008Provider发出的sql:

which successfully executes the query. However the sql emitted by the Sql2008Provider:

SELECT [t0].[Id]
FROM [dbo].[TimeBlock] AS [t0]
WHERE DATEADD(ms, (CONVERT(BigInt,@p0 * 86400000)) % 86400000, DATEADD(day, (CONVERT(BigInt,@p0 * 86400000)) /
 86400000, CONVERT(DATE, [t0].[StartDate]))) <= @p1

包含导致异常的错误sql.

Contains the erroneous sql that causes the exception.

我是否认为是Sql提供程序导致了此问题?

Am I right in thinking that the it is the Sql provider that is causing this problem?

是否有一种方法可以通过强制DataContext为此数据库使用Sql2005Provider来解决此问题?

Is there a way we can get round this problem by forcing the DataContext to use the Sql2005Provider for this db?

感谢您能为我们提供的任何帮助!

Thanks for any help you can give us!

推荐答案

在我看来,您发现LINQ to SQL中的错误.您应该将其报告给Microsoft.您可以在此处执行以下操作: http://connect.microsoft.com/

It seems to me you found a bug in LINQ to SQL. You should report it to Microsoft. You can do that here: http://connect.microsoft.com/

这篇关于我可以强制Linq to Sql使用Sql2005Provider吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆