正在使用的 SQL Server 版本不支持数据类型 datetime2?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10224406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The version of SQL Server in use does not support datatype datetime2?
提问by Barbara88
An error occurred while executing the command definition. See the inner exception for details. bbbbInnerException:aaaa System.ArgumentException: The version of SQL Server in use does not support datatype 'datetime2'.
at System.Data.SqlClient.TdsParser.TdsExecuteRPC(_SqlRPC[] rpcArray, Int32 timeout, Boolean inSchema, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj, Boolean isCommandProc)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavioR
I have a website using Entity Framework. A few months ago I added a new table, and added some columns to existing tables; everything worked fine.
我有一个使用实体框架的网站。几个月前,我添加了一个新表,并在现有表中添加了一些列;一切正常。
Today I updated the mapping of the EDMX so the new table and the new column can be used, and added WebMethods to my services.asmx file. Since then I cannot run my site because I have that error that I cannot understand. Please explain it to me if you understand, and tell me where is my mistake.
今天我更新了 EDMX 的映射,以便可以使用新表和新列,并将 WebMethods 添加到我的 services.asmx 文件中。从那以后,我无法运行我的网站,因为我遇到了无法理解的错误。如果您理解,请向我解释,并告诉我我的错误在哪里。
I have not used datetime2anywhere. There is no such datatype in my new table, nor in the columns that I added to existing tables.
我没有在任何地方使用过datetime2。我的新表中没有这样的数据类型,我添加到现有表中的列中也没有。
The version of SQL on my PC is SQL2008 R2, on the server i have SQL2008. I do not have the option to upgrade the server to R2.
我电脑上的SQL版本是SQL2008 R2,服务器上我有SQL2008。我没有将服务器升级到 R2 的选项。
采纳答案by Ladislav Mrnka
In addition to @Mithrandir answer validate that your database is running in compatibility level set to 100 (SQL 2008).
除了@Mithrandir 回答之外,请验证您的数据库是否以设置为 100 (SQL 2008) 的兼容性级别运行。
You don't have to use DATETIME2
in your database to get this error. This error happens usually once you add required (NOT NULL
) DATETIME
column to existing table and you don't set the value prior to saving the entity to database. In such case .NET will send default value which is 1.1.0001 and this value doesn't fit into DATETIME
range. This (or something similar) will be source of your problem.
您不必DATETIME2
在数据库中使用来获取此错误。一旦您将 required ( NOT NULL
)DATETIME
列添加到现有表并且在将实体保存到数据库之前没有设置值,通常会发生此错误。在这种情况下,.NET 将发送默认值 1.1.0001,该值不适合DATETIME
范围。这(或类似的东西)将是您的问题的根源。
回答by Mithrandir
Have you tried to open your EDMX file with XML Editor and check the value of ProviderManifestToken
. It may help to change from ProviderManifestToken=”2008”
to ProviderManifestToken=”2005”
.
您是否尝试过使用 XML 编辑器打开 EDMX 文件并检查ProviderManifestToken
. 它可以帮助改变ProviderManifestToken=”2008”
来ProviderManifestToken=”2005”
。
回答by Maher Ben Issa
Open your EDMX in a file editor (or “open with…” in Visual Studio and select XML Editor). At the top you will find the storage model and it has an attribute ProviderManifestToken. This has should have the value 2008. Change that to 2005, recompile and everything works.
在文件编辑器中打开您的 EDMX(或在 Visual Studio 中“打开方式...”并选择 XML 编辑器)。在顶部,您会找到存储模型,它有一个属性 ProviderManifestToken。这应该具有值 2008。将其更改为 2005,重新编译,一切正常。
NOTE: You'll have to do this every time you update the model from database.
注意:每次从数据库更新模型时,您都必须这样做。
回答by Ben Anderson
The other solutions worked for me but I needed a more permanent solution that would not be reverted every time the edmx was updated from the database. So I created a "Pre-build event" to modify the ProviderManifestToken automatically.
其他解决方案对我有用,但我需要一个更持久的解决方案,每次从数据库更新 edmx 时都不会恢复该解决方案。所以我创建了一个“预构建事件”来自动修改 ProviderManifestToken。
Link to original answer: https://stackoverflow.com/a/8764394/810850
链接到原始答案:https: //stackoverflow.com/a/8764394/810850
The prebuild step looks like this:
预构建步骤如下所示:
$(SolutionDir)Artifacts\SetEdmxVer\SetEdmxSqlVersion $(ProjectDir)MyModel.edmx 2005
The code is here:
代码在这里:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace SetEdmxSqlVersion
{
class Program
{
static void Main(string[] args)
{
if (2 != args.Length)
{
Console.WriteLine("usage: SetEdmxSqlVersion <edmxFile> <sqlVer>");
return;
}
string edmxFilename = args[0];
string ver = args[1];
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(edmxFilename);
XmlNamespaceManager mgr = new XmlNamespaceManager(xmlDoc.NameTable);
mgr.AddNamespace("edmx", "http://schemas.microsoft.com/ado/2008/10/edmx");
mgr.AddNamespace("ssdl", "http://schemas.microsoft.com/ado/2009/02/edm/ssdl");
XmlNode node = xmlDoc.DocumentElement.SelectSingleNode("/edmx:Edmx/edmx:Runtime/edmx:StorageModels/ssdl:Schema", mgr);
if (node == null)
{
Console.WriteLine("Could not find Schema node");
}
else
{
Console.WriteLine("Setting EDMX version to {0} in file {1}", ver, edmxFilename);
node.Attributes["ProviderManifestToken"].Value = ver;
xmlDoc.Save(edmxFilename);
}
}
}
}
回答by Walter Stabosz
Code First workaround.
代码优先解决方法。
I got this error while running a linq select query, and changing the EDMX isn't an option for me (Code First has no EDMX), and I didn't want to implement this How to configure ProviderManifestToken for EF Code Firstfor a Linqpad query that wasn't going into production code:
我在运行 linq 选择查询时遇到此错误,更改 EDMX 对我来说不是一个选项(Code First 没有 EDMX),而且我不想实现这个How to configure ProviderManifestToken for EF Code Firstfor a Linqpad未进入生产代码的查询:
// [dbo].[People].[Birthday] is nullable
DateTime minBirthday = DateTime.Now.AddYears(-18);
var query =
from c in context.People
where c.Birthday > birthday
select c;
var adults = query.ToList();
I fixed it by changing query
to null check first:
我通过先更改query
为空检查来修复它:
var query =
from c in context.People
where (c.Birthday.HasValue && (c.Birthday > birthDay) )
select c;