c# - 在单个 Web API 请求中使用 MySQL 和 SQL Server 上下文

标签 c# mysql sql-server ninject asp.net-web-api2

我有一个带有自定义 AuthHandler 的 Web 应用程序,该应用程序使用 MySQL 连接来检查权限和业务逻辑,该应用程序链接到 MS SQL 数据库实例实体

因此,当创建 MS SQL 上下文时会出现问题:

System.ArgumentException
Additional information: Keyword not supported.
   at MySql.Data.MySqlClient.MySqlConnectionStringBuilder.GetOption(String key)
   at MySql.Data.MySqlClient.MySqlConnectionStringBuilder.set_Item(String keyword, Object value)
   at System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)
   at MySql.Data.MySqlClient.MySqlConnectionStringBuilder..ctor(String connStr)
   at MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString(String value)
   at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<SetConnectionString>b__18(DbConnection t, DbConnectionPropertyInterceptionContext`1 c)

上下文代码:

public SqlServerContext()
    :base("SQLServer")
{
    this.Configuration.LazyLoadingEnabled = false;
    this.Configuration.AutoDetectChangesEnabled = true;
}

public MySqlContext()
    :base("MySQL")
{
}

配置中的连接字符串:

<add name="SQLServer"
     connectionString="Data Source={};Initial Catalog={};Persist Security Info=True;User ID={};Password={};MultipleActiveResultSets=True"
     providerName="System.Data.SqlClient" />

<add name="MySQL" connectionString="Server={};Port=3306;Database=Test;Uid={};Pwd={}" providerName="MySql.Data.MySqlClient" />

<providers>
    <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
</providers>

现在使用 Ninject 容器注入(inject)上下文:

    binder.Bind<DbContext>().ToConstructor(i => new SqlServerContext()).Named(GlobalConstants.Context.ContentCodename);
    binder.Bind<DbContext>().ToConstructor(i => new MySqlContext()).Named(GlobalConstants.Context.UserCodename);

首先,我不明白为什么代码试图使用 MySql 实例构建连接字符串。所以我决定使用隐式连接字符串创建 SQL Server 上下文:

public SqlServerContext()
    : base(a())
{
    this.Configuration.LazyLoadingEnabled = false;
    this.Configuration.AutoDetectChangesEnabled = true;
}

private static string a()
{
    var a = new EntityConnectionStringBuilder
    {
        Provider = "System.Data.SqlClient",
        ProviderConnectionString = new SqlConnectionStringBuilder
        {
            DataSource = @"{}",
            InitialCatalog = "{}",
            UserID = "{}",
            Password = "{}",
            MultipleActiveResultSets = true
        }.ConnectionString
    }.ConnectionString;
    return a;
}

但存在类似以下错误:不支持提供程序名称关键字不支持多个事件结果集关键字 - 这是 MySQL 行为的特征。有没有人解决这个问题?

最佳答案

MySQL 上下文由属性标记:

[DbConfigurationType(typeof (MySqlEFConfiguration))]

建议在使用 EF 的 MySQL 上下文中使用此方法。删除此属性解决问题

关于c# - 在单个 Web API 请求中使用 MySQL 和 SQL Server 上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30350358/

相关文章:

java - 长时间发送请求时出现 org.springframework.transaction.CannotCreateTransactionException

php - 通过 ORM 进行 Magento SELECT

MYSQL KEY-VALUE PAIR 生存能力

c# - 端口是从 SQL Server 自动收集的吗?

c# - MVC .NET OrWhere 查询

c# - 如何从 IOwinRequest 获取上传文件的集合?

c# - 使用 WPF 网页浏览器和带有斜体标签和阿拉伯文本的 HTML 时出现奇怪的字符

c# - 在 ASP.Net 中注册事件的最佳位置

mysql - 使用分隔符将 SQL 数据拆分为 3 列

sql - SQL 连接使用多个 'ON' 关键字