25
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 

我有一个网站使用实体框架。几个月前,我添加了一个新表格,并在现有表格中添加了一些列;一切正常。正在使用的SQL Server版本不支持数据类型datetime2?

今天我更新了EDMX的映射,以便可以使用新表和新列,并将WebMethods添加到我的services.asmx文件中。从那时起,我无法运行我的网站,因为我有我无法理解的错误。如果你明白,请向我解释,并告诉我我的错误在哪里。

我还没有用过任何地方的datetime2。新表格中没有这种数据类型,也没有添加到现有表格中的列中。

我的电脑上的SQL版本是SQL2008 R2,在我有SQL2008的服务器上。我没有选择将服务器升级到R2。

+0

您正在使用CLR过程? – Milee 2012-04-19 09:02:33

+0

[Datetime2](http://msdn.microsoft.com/zh-cn/library/bb677335%28v=sql.100%29.aspx)在SQL 2008中确实存在,所以我认为在服务器上升级到R2会无论如何,你无法帮助你。 – Bridge 2012-04-19 09:04:30

+0

看看你的数据库的兼容级别(属性 - >选项),它是否也是SQL Server 2008? – Wim 2012-04-19 09:11:13

回答

18

除@Mithrandir答案验证您的数据库正在以兼容级别设置为100(SQL 2008)运行。

您不必在数据库中使用DATETIME2即可获取此错误。通常在将所需的(NOT NULLDATETIME列添加到现有的表中并且在将实体保存到数据库之前未设置该值时,通常会发生此错误。在这种情况下,.NET将发送默认值1.1.0001,该值不适合DATETIME范围。这个(或类似的东西)将成为你的问题的根源。

+0

我检查了我的兼容性级别,它是90(2005)。还检查了我的脚本,它确实在datetime字段上为空。但仍然有这个错误信息。顺便说一下,我正在使用SQL Server 2012 – Franva 2014-11-03 03:52:34

76

您是否试图用XML Editor打开您的EDMX文件并检查ProviderManifestToken的值。这可能有助于从ProviderManifestToken=”2008”更改为ProviderManifestToken=”2005”

+3

为我完美工作。虽然上面的答案给出了原因,但这个答案很快就能解决。 – aokelly 2013-06-20 21:27:20

+2

这很好 - 很快,我爱你。 – 2013-11-18 12:10:01

+0

这工作对我来说 – 2013-12-16 20:41:19

12

在文件编辑器中打开您的EDMX(或在Visual Studio中打开并选择XML Editor)。在顶部你会找到存储模型,它有一个属性ProviderManifestToken。这应该有价值2008.更改到2005年,重新编译,一切正常。

注意:每次从数据库更新模型时都必须执行此操作。

3

其他解决方案适用于我,但我需要一个更永久的解决方案,每次从数据库更新edmx时都不会恢复。所以我创建了一个“预生成事件”来自动修改ProviderManifestToken。

链接到原来的答案: https://stackoverflow.com/a/8764394/810850

的预生成的步骤是这样的:

$(SolutionDir)Artifacts\SetEdmxVer\SetEdmxSqlVersion $(ProjectDir)MyModel.edmx 2005 

的代码是在这里:

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); 
      } 
     } 
    } 
} 
+0

这与我如何自动化它相似。唯一的区别是我写了一个工具来解析和设置任何XML标签/属性,而不是一个特定的。自动构建有更多用途... – 2015-04-27 19:55:02

0

代码第一个解决方法。

我在运行linq select查询时遇到了这个错误,并且更改EDMX对我来说不是一个选项(Code First没有EDMX),而且我也不想为Linqpad查询实现这个How to configure ProviderManifestToken for EF Code First, “T进入生产代码:

// [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(); 

我固定它通过改变query为null检查第一:

var query = 
    from c in context.People 
    where (c.Birthday.HasValue && (c.Birthday > birthDay)) 
    select c; 
相关问题