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

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

我正在尝试在同一控制台应用程序上从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#代码中禁用entityFramework和MySQL代码块中的app.config部分,我可以毫无问题地从SQL Server检索数据。

版本信息

  • MySQL.Data.Entity 6.10.8
  • NET Framework 4.6.1

任何的想法?


更新1

发现MySQLDb的连接类型是MySql.Data.MySqlClient.MySqlConnection,所以工作得很好。但是当实例化SQLDb时,连接类型仍然是MySql.Data.MySqlClient.MySqlConnection而不是System.Data.SqlClient.SqlConnection。我们该如何解决这个问题?

c# mysql sql-server entity-framework-6
1个回答
0
投票

问题是我们正在使用MySql.Data.Entity.MySqlEFConfiguration(在app.config中)将默认连接工厂设置为使用MySqlConnectionFactory

解决方案是使用自定义DbConfiguration代替MySql.Data.Entity.MySqlEFConfiguration来阻止设置默认连接工厂。

public class MySQLDbConfiguration : DbConfiguration
{
    public MySQLDbConfiguration()
    {
        SetProviderServices(MySqlProviderInvariantName.ProviderName, new MySqlProviderServices());
        SetProviderFactory(MySqlProviderInvariantName.ProviderName, new MySqlClientFactory());
    }
}

在代码中的某个地方将实例声明为readonly,

private static readonly MySQLDbConfiguration DBConfig = new MySQLDbConfiguration();

并使用任何EF功能设置PRIOR TO配置

DbConfiguration.SetConfiguration(DBConfig);

而我们的app.config现在变成了

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <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>
</configuration>

如果您选择使用app.config而不是派生自定义DbConfiguration,您可以执行以下操作

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

  <!-- Alternative to custom DbConfiguration. -->
  <configSections>
    <section name = "entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <providers>
      <provider invariantName = "MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/>
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant = "MySql.Data.MySqlClient" />
      <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"/>
    </DbProviderFactories>
  </system.data>

  <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>
</configuration>
© www.soinside.com 2019 - 2024. All rights reserved.