将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检索数据而没有任何问题。

版本信息

  • MySQL.Data.Entity 6.10.8
  • NET Framework 4.6.1

任何想法?


更新1

发现,连接类型MySQLDbMySql.Data.MySqlClient.MySqlConnection,这样的作品就好了。但是在实例化时SQLDb,连接类型仍然MySql.Data.MySqlClient.MySqlConnectionSystem.Data.SqlClient.SqlConnection我们应该如何解决呢?

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

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

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

在代码的某处将实例声明为只读,

private static readonly MySQLDbConfiguration DBConfig = new MySQLDbConfiguration();

并在使用任何EF功能之前设置配置

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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将JDBI与不支持预准备语句的JDBC驱动程序一起使用

Xcode 6 / Beta 4:不支持将桥接标头与框架目标一起使用

将选项卡与ViewPager一起使用时出现错误“ Java.lang.IllegalStateException活动已被破坏”

解决将OnModelCreating与ApplicationDbContext一起使用时出现的“未定义键”错误?

将Typescript与React-Redux一起使用时出现类型错误

java.sql.SQLException:将HikariCP与Hive JDBC一起使用时不支持该方法

将Pandoc与Markdown一起使用时出现紧缩列表错误

将拆分与数据帧一起使用时出现未找到的错误

将net-ssh与可与ssh命令一起使用的选项一起使用时,ConnectionTimeout错误?

与MySQL一起使用的SQLAlcemy关系与Sql Server一起使用时会生成错误

将std :: invoke_result_t与通用lambda一起使用时出现硬错误

将SQL与INSERT INTO一起使用时出现错误1064 ... SELECT ...重复键更新

将Wasm捆绑在一起后,为什么会出现错误“响应具有不受支持的MIME类型”,但在与webpack开发服务器一起使用时却不会出现错误?

将`getopt_long`与无法识别的long选项一起使用时出现分段错误

gRPC和ASP Net Core:GrpcChannel不支持将SslCredentials与非null参数一起使用

将模糊搜索与猫鼬一起使用时出现打字稿错误

将pynput与pyinstaller一起使用时出现错误

将EclEmma(eclipse插件)与JMockit一起使用时出现错误(仅与Delegate()一起使用)

将Scanner与Double一起使用时出现Java错误

当将`track by $ index`与过滤器一起使用时,出现错误。怎么解决呢?

将cx_freeze与pythoncom一起使用时出现导入错误

将 DQL 与 symfony 一起使用时出现 SQL 错误

将撇号与 materialize 一起使用时出现类型错误

将排序与 seq::index::sample 一起使用时出现 Rust 错误

将 pyyaml 与 FastAPI 一起使用时出现 CORS 错误

将 Sphinx 与 Django 一起使用时出现 Autodoc 错误

将 Tkinter 与 SQLite 一起使用时收到错误消息,“可能不受支持的类型”。

将 HttpServlet 与 JAVA 一起使用时出现关闭错误

协议不支持地址系列在将 Scapy L3socket 与 WSL 一起使用时