将 SQL Server 与 MySQL 一起使用时出现“不支持的选项”错误

我试图在同一个控制台应用程序上从 MySQL 和 SQL Server 检索数据。我设法从 MySQL 检索数据,但是当我尝试从 SQL Server 检索数据时,出现System.ArgumentException: 'Option not supported. Parameter name: multipleactiveresultsets'错误。


以下是我的app.config:


<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <configSections>

    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

  </configSections>

  <startup>

    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />

  </startup>


  <connectionStrings>

    <add name="MySQLDb" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;port=3306;database=sakila;uid=some_user;password=some_password"/>

    <add name="SQLDb" providerName="System.Data.SqlClient" connectionString="data source=USER-PC\SQLEXPRESS;initial catalog=MyDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"/>

  </connectionStrings>


  <entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">

    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>

    <providers>

      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/>

      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>

    </providers>

  </entityFramework>

</configuration>

还有我的 C#:


#region MySQL.

{

    var dbContext = new MySQLDb();

    var dbSet = dbContext.Set<Actor>();


    var actors = dbSet.ToList();

}

#endregion


#region SQLServer.

{

    var dbContext = new SQLDb();

    var dbSet = dbContext.Set<User>();


    var users = dbSet.ToList(); // <-- Throw exception.

}

#endregion

如果我在 C# 代码中禁用entityFrameworksection inapp.config和 MySQL 代码块,我可以毫无问题地从 SQL Server 检索数据。


版本信息


斯蒂芬大帝
浏览 437回答 2
2回答

潇湘沐

问题是我们正在使用MySql.Data.Entity.MySqlEFConfiguration(在 中app.config)将默认连接工厂设置为使用MySqlConnectionFactory。解决方案是使用自定义DbConfiguration代替MySql.Data.Entity.MySqlEFConfiguration来阻止设置默认连接工厂。public class MySQLDbConfiguration : DbConfiguration{&nbsp; &nbsp; public MySQLDbConfiguration()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; SetProviderServices(MySqlProviderInvariantName.ProviderName, new MySqlProviderServices());&nbsp; &nbsp; &nbsp; &nbsp; SetProviderFactory(MySqlProviderInvariantName.ProviderName, new MySqlClientFactory());&nbsp; &nbsp; }}在代码中的某处将实例声明为只读,private static readonly MySQLDbConfiguration DBConfig = new MySQLDbConfiguration();并在使用任何 EF 功能之前设置配置DbConfiguration.SetConfiguration(DBConfig);而我们的app.config现在变成<?xml version="1.0" encoding="utf-8" ?><configuration>&nbsp; <startup>&nbsp; &nbsp; <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />&nbsp; </startup>&nbsp; <connectionStrings>&nbsp; &nbsp; <add name="MySQLDb" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;port=3306;database=sakila;uid=some_user;password=some_password"/>&nbsp; &nbsp; <add name="SQLDb" providerName="System.Data.SqlClient" connectionString="data source=USER-PC\SQLEXPRESS;initial catalog=MyDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"/>&nbsp; </connectionStrings></configuration>如果您选择使用app.config而不是派生自定义DbConfiguration,则可以执行以下操作<?xml version="1.0" encoding="utf-8" ?><configuration>&nbsp; <!-- Alternative to custom DbConfiguration. -->&nbsp; <configSections>&nbsp; &nbsp; <section name = "entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />&nbsp; </configSections>&nbsp; <entityFramework>&nbsp; &nbsp; <providers>&nbsp; &nbsp; &nbsp; <provider invariantName = "MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/>&nbsp; &nbsp; </providers>&nbsp; </entityFramework>&nbsp; <system.data>&nbsp; &nbsp; <DbProviderFactories>&nbsp; &nbsp; &nbsp; <remove invariant = "MySql.Data.MySqlClient" />&nbsp; &nbsp; &nbsp; <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.10.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>&nbsp; &nbsp; </DbProviderFactories>&nbsp; </system.data>&nbsp; <startup>&nbsp; &nbsp; <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />&nbsp; </startup>&nbsp; <connectionStrings>&nbsp; &nbsp; <add name="MySQLDb" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;port=3306;database=sakila;uid=some_user;password=some_password"/>&nbsp; &nbsp; <add name="SQLDb" providerName="System.Data.SqlClient" connectionString="data source=USER-PC\SQLEXPRESS;initial catalog=MyDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"/>&nbsp; </connectionStrings></configuration>

富国沪深

如果您使用的是 asp net 5.0,请安装此处找到的 Nuget&nbsp;,但首先要卸载 Mysql.data。
打开App,查看更多内容
随时随地看视频慕课网APP