实体框架 - 使用其他 exe 的配置文件

本文关键字:exe 配置文件 其他 框架 实体 | 更新日期: 2023-09-27 18:34:09

我有两个与同一个数据库交互的exe。一个是主应用程序,另一个是更新程序应用程序(更新主应用程序)。我试图将安装包限制为一个配置文件。IE 我希望更新程序应用程序使用主应用程序配置文件。

我能够拉取连接字符串并创建数据库上下文,如下所示:

string entityConnectionString = (entitySettings == null) ? "" : entitySettings.ConnectionString;
var ecb = new EntityConnectionStringBuilder(entityConnectionString);
var ec = new EntityConnection(ecb.ToString());

但是,如果我不包括下面更新程序应用程序的配置,则会出现错误The specified store provider cannot be found in the configuration, or is not valid.

<system.data>
  <DbProviderFactories>
    <remove invariant="System.Data.SqlServerCe.4.0"/>
    <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.1, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
  </DbProviderFactories>
</system.data>

如何告诉实体框架或应用程序使用其他配置文件或绕过此错误消息?为更新程序 exe 添加配置文件不是一种选择,因为老板想要一个配置文件。(我们希望能够远程更新配置文件,添加多个配置文件会使其更加复杂。

实体框架 - 使用其他 exe 的配置文件

您可以

允许多个项目共享相同的app.config,如下所示:

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"C:'Shared'app.config");

最终使用了这个答案:

添加不带 App.Config 的 DbProviderFactory

通过从主 exe.config 中提取数据并插入值。