在使用Visual Studio安装程序的安装过程中更改app.configuration文件失败:无法加载文件或程序集'EntityFramework'

问题描述 投票:0回答:1

项目信息:我们正在创建一个安装程序项目,允许用户选择服务器名和数据库名。安装主程序后,将创建一个相应的数据库。

安装程序项目:

  • 具有带有两个文本框的附加UI屏幕,其中包含服务器名和数据库名称。
  • 自定义操作将在安装时执行,并将服务器名和数据库名定义为自定义操作数据。

主要应用:

  • 安装程序类连接安装后事件。该类将在安装过程中自动执行。自定义操作数据将作为参数传递给此类。
  • 安装后事件将触发以下方法:
    • 创建数据库
    • 获取app.config文件并调整连接字符串。

一切正常期望保存配置文件:我们收到此错误:

System.Configuration.ConfigurationErrorsException:创建> EntityFramework的配置节处理程序时发生错误:无法加载文件或程序集'EntityFramework,Version = 6.0.0.0,Culture = neutral,> PublicKeyToken = b77a5c561934e089'

奇怪的是,在运行和调试主应用程序时,Entityframwork不会出现任何错误。此外,我们还能够将代码作为unittest执行,因此仅在使用Visual Studio安装程序进行安装期间更改配置文件时,才会出现此组装问题。

下面您可以找到更改配置文件的代码。

void DeployInstaller_AfterInstall(object sender, InstallEventArgs e)
{
  try
  {
    Configuration config = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);

    string connectionsection = config.ConnectionStrings.ConnectionStrings
    ["XBSDbDataContext"].ConnectionString;

    ConnectionStringSettings connectionstring = null;
    if (connectionsection != null)
    {
          config.ConnectionStrings.ConnectionStrings.Remove("XBSDbDataContext");
    }

    connectionstring = new ConnectionStringSettings("XBSDbDataContext", connectionString);
    config.ConnectionStrings.ConnectionStrings.Add(connectionstring);

    config.Save(ConfigurationSaveMode.Minimal, true);
    }
    catch (Exception ex)
    {
          MessageBox.Show(ex.ToString());      
    }
}

任何想法可能是什么原因,如何解决?提前非常感谢。

-更新-我们发现了一项工作。

避免使用ConfigurationManager类来编辑配置文件,我们不再有这个问题。现在,这是通过system.xml完成​​的命名空间。

  //updating config file
  XmlDocument XmlDoc = new XmlDocument();
  MessageBox.Show(Assembly.GetExecutingAssembly().Location + ".config");
  XmlDoc.Load(Assembly.GetExecutingAssembly().Location+".config");
  foreach (XmlElement xElement in XmlDoc.DocumentElement)
  {
       if (xElement.Name == "connectionStrings")
       {
            xElement.LastChild.Attributes["connectionString"].Value = connectionString;
       }
  }
  XmlDoc.Save(Assembly.GetExecutingAssembly().Location + ".config");

项目信息:我们正在创建一个安装程序项目,允许用户选择服务器名和数据库名。安装主程序后,将创建一个相应的数据库。 ...

c# entity-framework windows-installer installer app-config
1个回答
0
投票

如果您的MSI正在将该Dll(或它的依赖项)安装到GAC,则问题在于,直到安装的Commit阶段,才能在GAC中实际访问已安装的GAC程序集。尽管名称为“ AfterInstall”,但该事件实际上是“安装即将结束”。如果您将该自定义操作移动为“提交”自定义操作并且可以正常工作,那就是问题所在。

© www.soinside.com 2019 - 2024. All rights reserved.