简体   繁体   中英

Using MySQL and SQL Server contexts in single Web API request

i have a web application with custom AuthHandler that uses MySQL connection for checking permissions and business logic, that is linked to MS SQL database instance entities

so, the problem happens when the MS SQL Context is creating:

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)

contexts code:

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

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

connection strings in config:

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

contexts are now injected with Ninject container:

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

for the first, i don't understant, why code is trying to build connection strings with MySql instances. so i've decided to create SQL Server context with implicit connection string:

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

but there are errors something like: provider name keyword is not supported , multiple active results set keyword is not supported - that is characteristically for MySQL behavior. does have anybody solutions for that problem?

MySQL context was marked by attribute:

[DbConfigurationType(typeof (MySqlEFConfiguration))]

that is recommended for MySQL contexts using EF. removing this attribute solving issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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