更改配置中的实体框架提供程序

发布于 2024-10-18 14:58:56 字数 243 浏览 7 评论 0原文

我希望能够通过配置更改在 SQL Server 和 SQL Server Compact Edition 之间切换数据提供程序。但它不起作用,查看 EDMX 文件,我想我可以明白为什么:

<edmx:StorageModels>
<Schema ... Provider="System.Data.SqlClient" ...

有没有办法在 app.config 或运行时指定提供程序?

I was hoping to be able to switch data providers to/from SQL Server and SQL Server Compact Edition via just a configuration change. But it doesnt work and looking at the EDMX file I think I can see why:

<edmx:StorageModels>
<Schema ... Provider="System.Data.SqlClient" ...

Is there any way to specify the provider in app.config or at runtime?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

本王不退位尔等都是臣 2024-10-25 14:58:56

存储模型绑定到特定的提供程序,这将导致实体框架拒绝任何与指定提供程序不兼容的 DbConnection 实现。

如果查看实体框架连接字符串,您可以看到 StorageSchema、ModelSchema 和 Mapping 在三个不同的文件中指定(这些文件是从 .edmx 生成并嵌入到程序集中)。您可以将 .edmx 拆开并自行嵌入 .ssdl、.csdl 和 .msl,然后为 SQL Server CE 创建另一个 .ssdl。这基本上只是复制和复制。粘贴并替换提供者和一些列类型。

我在这里写过:比较实体框架

The Storage-Model is tied to a specific provider, which will cause the Entity Framework to reject any DbConnection implementations that are not compatible with the specified provider.

If you look at an Entity Framework connection-string, you can see that the StorageSchema, ModelSchema and Mapping are specified in three different files (which are generated from your .edmx and than embedded in the assembly). You could take your .edmx apart and embed the .ssdl, .csdl and .msl yourself and than create another .ssdl for SQL Server CE. This is basically just copy & paste and replacing the provider and some column-types.

I wrote about here: Comparison Entity Framework

昔梦 2024-10-25 14:58:56

对于单元测试,我以这种方式更改架构(在主代码执行之前更改 ssdl)。

在代码中:

    var s = Assembly.GetExecutingAssembly().GetManifestResourceStream("Model1.ssdl");
    var ssdlFilePath = "<some-dir>\file1.ssdl";
    using (var file = File.Create(ssdlFilePath))
    {
        StreamUtil.Copy(s, file);
    }
    var str = File.ReadAllText(ssdlFilePath);
    str = str.Replace("old provider token", "ProviderManifestToken=\"4.0\"");
    str = str.Replace("old provider type"", "Provider=\"System.Data.SqlServerCe.4.0\"");
    File.WriteAllText(ssdlFilePath, str);

在 app.config 中:

  <connectionStrings>
    <add name="Database2Entities" connectionString="metadata=res://*/Model1.csdl|<some-dir>\file1.ssdl|res://*/Model1.msl;provider=System.Data.SqlServerCe.4.0;provider connection string="Data Source=|DataDirectory|\Database1.sdf"" providerName="System.Data.EntityClient" />
  </connectionStrings>

它有效)

For Unit Tests I change Schema this way (changing ssdl before main code execution).

In code:

    var s = Assembly.GetExecutingAssembly().GetManifestResourceStream("Model1.ssdl");
    var ssdlFilePath = "<some-dir>\file1.ssdl";
    using (var file = File.Create(ssdlFilePath))
    {
        StreamUtil.Copy(s, file);
    }
    var str = File.ReadAllText(ssdlFilePath);
    str = str.Replace("old provider token", "ProviderManifestToken=\"4.0\"");
    str = str.Replace("old provider type"", "Provider=\"System.Data.SqlServerCe.4.0\"");
    File.WriteAllText(ssdlFilePath, str);

In app.config:

  <connectionStrings>
    <add name="Database2Entities" connectionString="metadata=res://*/Model1.csdl|<some-dir>\file1.ssdl|res://*/Model1.msl;provider=System.Data.SqlServerCe.4.0;provider connection string="Data Source=|DataDirectory|\Database1.sdf"" providerName="System.Data.EntityClient" />
  </connectionStrings>

It works)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文