相同的EDMX文件为不同的提供商 [英] Same EDMX file for different Providers
问题描述
我在一个项目中,我有一个本地数据库(SQL CE),它被用作一种缓冲区,而没有与服务器的连接存在。在服务器上,我想使用相同的数据库布局。
I'm working on a project where I have a local database (SQL CE) which is used as sort of a buffer while no connection to the server exists. On the server I want to use the same database-layout.
当然,我想使用在服务器上可用的Common.dll中的相同EDMX文件和客户端。
Of course I want to use the same EDMX-File which is in a Common.dll available on the Server and the Client.
在客户端中,我有一个连接字符串,其中provider = System.Data.SqlServerCe.3.5,而server = System.Data.SqlClient在服务器上
In the Client I have a connection string with provider=System.Data.SqlServerCe.3.5 while it is provider=System.Data.SqlClient on the server.
当我想在服务器端保存sth时,我的问题出现在:提供者清单不是System.Data.SqlClient.SqlProviderManifest类型。
My problem comes when I want to save sth on the Server-side: "The provider manifest given is not of type 'System.Data.SqlClient.SqlProviderManifest'."
甚至有机会在两个部分使用相同的EDMX-File?
或者有没有最佳实践如何处理这样的星座?
Is there even a chance to use the same EDMX-File on both parts? Or is there any best practice how to handle such a constellation?
谢谢你的帮助!
编辑:
我的主要问题是:是否可以使EDMX文件与不同的提供商一起使用?在我的情况下System.Data.SqlServerCe.3.5和System.Data.SqlClient!
My main Question is: Is it possible to enable an EDMX File to use with different Providers? In my case System.Data.SqlServerCe.3.5 and System.Data.SqlClient!
推荐答案
我们有一个确切的方案生产应用。我们使用T4模板根据规范的SQL EDMX文件生成SQLCE EDMX文件。这会留下2个EDMX文件,您可以在实例化单个上下文之间切换。
We have the exact scenario working on a production application. We used a T4 template to generate a SQLCE EDMX file based on a canonical SQL EDMX file. This leaves you with 2 EDMX files which you can switch between when instantiating your single context....
serverType = "sqlserverce" (or) "sqlserver";
var entityBuilder = new EntityConnectionStringBuilder
{
Provider = ...,
ProviderConnectionString = ...,
Metadata = string.Format("res://*/{0}.{1}.csdl|res://*/{0}.{1}.ssdl|res://*/{0}.{1}.msl", EdmxName, serverType)
};
SQLCE EDMX T4代码看起来像这样...
The SQLCE EDMX T4 code looks like this...
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".edmx" #>
<#@ assembly name="System.Xml.dll" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Xml" #>
<#
var document = new XmlDocument();
document.Load(this.Host.ResolvePath("DbContext.edmx"));
var namespaceManager = new XmlNamespaceManager(document.NameTable);
namespaceManager.AddNamespace("edmx", "http://schemas.microsoft.com/ado/2008/10/edmx");
namespaceManager.AddNamespace("ssdl", "http://schemas.microsoft.com/ado/2009/02/edm/ssdl");
var storageModelsNode = document.SelectSingleNode("//edmx:StorageModels", namespaceManager);
foreach (XmlElement schemaNode in storageModelsNode.SelectNodes("ssdl:Schema", namespaceManager))
{
schemaNode.SetAttribute("Provider", "System.Data.SqlServerCe.4.0");
schemaNode.SetAttribute("ProviderManifestToken", "4.0");
foreach (XmlElement propertyNode in schemaNode.SelectNodes("ssdl:EntityType/ssdl:Property[@Type='varbinary(max)']", namespaceManager))
{
propertyNode.SetAttribute("Type", "image");
}
foreach (XmlElement propertyNode in schemaNode.SelectNodes("ssdl:EntityType/ssdl:Property[@Type='varchar']", namespaceManager))
{
propertyNode.SetAttribute("Type", "nvarchar");
}
}
var stringBuilder = new StringBuilder();
using (var stringWriter = new StringWriter(stringBuilder))
using (var xmlWriter = new XmlTextWriter(stringWriter) { Formatting = Formatting.Indented })
{
document.WriteTo(xmlWriter);
}
Write(stringBuilder.ToString());
#>
这篇关于相同的EDMX文件为不同的提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!