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