【问题标题】:Error Mixed mode assembly against version 'v2.0.50727' .. when creating tables while running setup , Installer class针对版本'v2.0.50727'的错误混合模式程序集..在运行安装程序时创建表,安装程序类
【发布时间】:2026-02-18 18:40:01
【问题描述】:

我有窗口应用程序,已经制作了一些 custom action,编辑 userinterface 在安装应用程序时从用户检索值,并在创建表时进一步在安装程序类中获取以下错误消息

Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot
be loaded in the 4.0 runtime without additional configuration information.

我google了很多,发现在下面添加会解决问题(不适合我)

 <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    <supportedRuntime version="v2.0.50727"/>
  </startup>

以前我的 app.confg 看起来像

<startup>
      <supportedRuntime version="v2.0.50727"/>
  </startup>

代码:Installer1.cs

public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);
            string targetDirectory = Context.Parameters["targetdir"];
            string servername = Context.Parameters["dbservername"];
            string dbname = Context.Parameters["dbname"];
            string strconnectionstring = "Data Source='" + servername + "';Initial Catalog='" + dbname + "';Integrated Security=True";

            if (servername == "")
            {
                throw new InstallException("You did not Specify SQL Servername!");
            }

            else if (dbname == "")
            {
                throw new InstallException("You did not specify the database name!");
            }

            string exePath = string.Format("{0}abc..exe", targetDirectory);
            System.Configuration.Configuration conf = ConfigurationManager.OpenExeConfiguration(exePath);
            conf.ConnectionStrings.ConnectionStrings["abc"].ConnectionString = strconnectionstring;
            conf.Save(ConfigurationSaveMode.Modified);


            using (SqlConnection con1 = new SqlConnection(strconnectionstring))
            {
                System.Diagnostics.Debugger.Break();
                string getScript = "Use " + dbname + "; CREATE TABLE supportContacts ( id int identity primary key,  type varchar(20),  details varchar(30)  )";
               // string strscript = getScript;
                Server server = new Server(new ServerConnection(con1));
                server.ConnectionContext.ExecuteNonQuery(getScript );
                con1.Close();
            }   


        }

调试时我知道错误发生在 server.ConnectionContext.ExecuteNonQuery(strupdatescript);

【问题讨论】:

  • 去掉supportedRuntime 并留下&lt;startup useLegacyV2RuntimeActivationPolicy="true"&lt;/startup 否则将所有引用定位到同一版本
  • @Ramhound:仍然出现同样的错误

标签: c# .net visual-studio-2010 installation


【解决方案1】:

对我来说,以下工作:

<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>

【讨论】:

  • 谢谢你的回复,我已经厌倦了这个,你可以检查我在我的问题中已经提到的问题,不要在我的情况下有什么问题。
【解决方案2】:

我遇到了同样的问题。如果有人能揭露什么可以解决这个问题,非常感谢。

我找到了/答案:)。为我工作。

http://reedcopsey.com/2011/09/15/setting-uselegacyv2runtimeactivationpolicy-at-runtime/

【讨论】: