27
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

我有一个使用实体框架的网站。几个月前,我添加了一个新表,并在现有表中添加了一些列;一切正常。

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

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

我PC上的SQL版本是SQL2008 R2,服务器上我有SQL2008。我没有将服务器升级到 R2 的选项。

4

5 回答 5

83
于 2012-04-19T09:18:21.137 回答
19

In addition to @Mithrandir answer validate that your database is running in compatibility level set to 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.

于 2012-04-19T09:47:03.460 回答
12

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.

NOTE: You'll have to do this every time you update the model from database.

于 2013-12-15T13:52:09.650 回答
3

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.

Link to original answer: 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);
            }
        }
    }
}
于 2014-03-24T16:09:23.970 回答
1

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 First for a Linqpad query that wasn't going into production code:

// [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:

var query =
    from c in context.People
    where (c.Birthday.HasValue && (c.Birthday > birthDay) )
    select c;
于 2018-02-07T17:00:40.743 回答