gpt4 book ai didi

c# - 将 SQL Server 与 MySQL 结合使用时出现“选项不支持”错误

转载 作者:行者123 更新时间:2023-11-29 16:08:58 25 4
gpt4 key购买 nike

我正在尝试在同一个控制台应用程序上从 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

如果我禁用entityFramework app.config 中的部分和我的 C# 代码中的 MySQL 代码块,我可以毫无问题地从 SQL Server 检索数据。

版本信息

  • MySQL.Data.Entity 6.10.8
  • NET框架4.6.1

有什么想法吗?

<小时/>

更新1

发现连接类型为MySQLDbMySql.Data.MySqlClient.MySqlConnection ,这样效果就很好。但是在实例化SQLDb时,连接类型仍然是MySql.Data.MySqlClient.MySqlConnection而不是System.Data.SqlClient.SqlConnection 。我们应该如何解决这个问题?

最佳答案

问题是我们正在使用 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());
}
}

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

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>

关于c# - 将 SQL Server 与 MySQL 结合使用时出现“选项不支持”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55448558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com