使用SQL Server 2008和SQL Server 2005和日期时间
-
11-07-2019 - |
题
我已经建立了针对2008数据库中的实体框架模型。所有的工作对2008数据库确定。当我尝试在2005年数据库更新实体我得到这个错误。
The version of SQL Server in use does not support datatype 'datetime2
我特别是当我建立了数据库,没有使用任何2008和功能。我找不到代码DATETIME2任何引用。而且,是在列定义为数据库中的“日期时间”。
解决方案
一个快速谷歌指出我是什么样子的的溶液。
(在Visual Studio和选择XML编辑器或“打开...”)在文件编辑器中打开EDMX。在顶部,你会发现存储模型,它有一个属性ProviderManifestToken。这有应有的价值2008修改成2005年,重新编译和一切正常。
请注意:你也会有这样的更新从数据库模型中的每个时间做
其他提示
线的快速查看:
<Schema Namespace="Foobar.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2005" >
这是非常令人沮丧,我很惊讶MS决定不让它那么你可以指定某个特定版本的SQL。为了确保我们的目标是2005年,我写了一个简单的控制台应用程序并调用它的预生成的一步。
在预生成步骤如下所示:
$(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);
}
}
}
}
使用@万斯的方便的控制台应用程序的上方,我使用了下列作为BeforeBuild事件
<Target Name="BeforeBuild">
<!--Check out BD.edmx, Another.edmx, all configs-->
<Exec Command="$(SolutionDir)\Library\tf checkout /lock:none $(ProjectDir)Generation\DB.edmx" />
<Exec Command="$(SolutionDir)\Library\tf checkout /lock:none $(ProjectDir)Generation\Another.edmx" />
<!--Set to 2008 for Dev-->
<Exec Condition=" '$(Configuration)' == 'DEV1' " Command="$(SolutionDir)Library\SetEdmxSqlVersion $(ProjectDir)Generation\DB.edmx 2008" />
<Exec Condition=" '$(Configuration)' == 'DEV1' " Command="$(SolutionDir)Library\SetEdmxSqlVersion $(ProjectDir)Generation\Another.edmx 2008" />
<Exec Condition=" '$(Configuration)' == 'DEV2' " Command="$(SolutionDir)Library\SetEdmxSqlVersion $(ProjectDir)Generation\DB.edmx 2008" />
<Exec Condition=" '$(Configuration)' == 'DEV2' " Command="$(SolutionDir)Library\SetEdmxSqlVersion $(ProjectDir)Generation\Another.edmx 2008" />
<!--Set to 2005 for Deployments-->
<Exec Condition=" '$(Configuration)' == 'TEST' " Command="$(SolutionDir)Library\SetEdmxSqlVersion $(ProjectDir)Generation\DB.edmx 2005" />
<Exec Condition=" '$(Configuration)' == 'TEST' " Command="$(SolutionDir)Library\SetEdmxSqlVersion $(ProjectDir)Generation\Another.edmx 2005" />
<Exec Condition=" '$(Configuration)' == 'PRODUCTION' " Command="$(SolutionDir)Library\SetEdmxSqlVersion $(ProjectDir)Generation\DB.edmx 2005" />
<Exec Condition=" '$(Configuration)' == 'PRODUCTION' " Command="$(SolutionDir)Library\SetEdmxSqlVersion $(ProjectDir)Generation\Another.edmx 2005" />
</Target>
<强>这是超级方便,因为避免了恼人的重新部署。感谢您分享万斯。强>
我添加TF.exe到图书馆解决方案文件夹,这会有所帮助,因为我现在可以尝试对它们进行编辑,作为构建的一部分之前检查出EDMX文件。此外,我与条件增加这一点,所以它设置为2005年部署到服务器,并回到2008年的开发机器SLN配置。另外提一下,你需要实际SetEdmxSqlVersion.exe(和.PDB)文件(S)添加到库文件夹(或其他任何地方,你想保持这些位)。
非常感谢@Vance。的很整洁,大量节省时间,并保持我的构建完全自动化和无痛苦:)
有关的谁遇到同样的问题,但使用人利益的代码首先,请我的答案这里有关如何更改代码首先ProviderManifestToken
。它涉及手动创建DbModelBuilder
以及使DbProviderInfo
实例(与适当的令牌)调用模型生成器的方法Build
时。
也有类似的问题,2012与2008。它可以使用XmlPeek和BeforeBuild事件来解决XmlPoke:
<Target Name="BeforeBuild">
<XmlPeek XmlInputPath="$(ProjectDir)MyModel.edmx"
Namespaces="<Namespace Prefix='edmx' Uri='http://schemas.microsoft.com/ado/2009/11/edmx'/><Namespace Prefix='ssdl' Uri='http://schemas.microsoft.com/ado/2009/11/edm/ssdl'/>"
Query="/edmx:Edmx/edmx:Runtime/edmx:StorageModels/ssdl:Schema/@ProviderManifestToken">
<Output TaskParameter="Result" ItemName="TargetedSQLVersion" />
</XmlPeek>
<XmlPoke Condition="@(TargetedSQLVersion) != 2008"
XmlInputPath="$(ProjectDir)MyModel.edmx"
Namespaces="<Namespace Prefix='edmx' Uri='http://schemas.microsoft.com/ado/2009/11/edmx'/><Namespace Prefix='ssdl' Uri='http://schemas.microsoft.com/ado/2009/11/edm/ssdl'/>"
Query="/edmx:Edmx/edmx:Runtime/edmx:StorageModels/ssdl:Schema/@ProviderManifestToken"
Value="2008">
</XmlPoke>
</Target>
如果你不喜欢自动更换,就可以简单地用一个错误的任务替换XmlPoke任务。
较好地解决了我是不是手动编辑EDMX文件在设计模式,并在上下文菜单只需打开EDMX“从数据库更新模型...”。 你必须要指向当然无论这是你的正确的SQL版本。
我们对SQL2005第3节,在这里我们没有它SQL2005第4节此错误。
添加SQL2005到连接字符串固定我们的具体问题。
我们还没有确定,为什么呢,并没有想修改代码以提供令牌上面解决(在部署过程中的问题表现)。