我们有在生产应用程序上工作的确切场景。我们使用 T4 模板基于规范的 SQL EDMX 文件生成 SQLCE EDMX 文件。这为您留下了 2 个 EDMX 文件,您可以在实例化单个上下文时在它们之间切换......
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 代码如下所示...
<#@ 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());
#>