繁体   English   中英

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

[英]Using MySQL and SQL Server contexts in single Web API request

我有一个带有自定义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容器注入上下文:

    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;
}

但是有类似以下错误: provider name keyword is not supported multiple active results set keyword is not supported -这是MySQL行为的特征。 有没有解决这个问题的人?

MySQL上下文由属性标记:

[DbConfigurationType(typeof (MySqlEFConfiguration))]

建议用于使用EF的MySQL上下文。 消除此属性解决问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM