0

ASP.NET MVC4 を使用しており、データ アクセス レイヤーを別のプロジェクトに移動しようとしています。サイトの各ユーザーは独自のデータベースを取得するため、各ユーザーの接続文字列で異なるデータベースを指定する必要があります。私が現在これを行っている方法は次のとおりです。

public TableDbContext GetTableDbContextForUser(string userName)
{
    MySqlConnection connection = new MySqlConnection(getUserConnectionString(userName));
    return new MySqlTableDbContext(connection);
}

private string getUserConnectionString(string userName)
{
    ConnectionStringSettings csSettings = ConfigurationManager.ConnectionStrings["MySqlConnection"];

    MySqlConnectionStringBuilder csBuilder = new MySqlConnectionStringBuilder(csSettings.ConnectionString);
    csBuilder.Database += "_" + userName;

    return csBuilder.GetConnectionString(true);
}

そして私のコンストラクタ:

public MySqlTableDbContext(MySqlConnection connection)
    : base(connection, false) //Inherits from DbContext
{ }

最後に、接続文字列 (これはメイン プロジェクトの Web.config とデータベース プロジェクトの App.config にあります):

<connectionStrings>
    <add name="MySqlConnection" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;User Id=root;Persist Security Info=True;database=cybercomm;password=*;DefaultCommandTimeout=0;" />
</connectionStrings>

MySqlTableDbContext は私のデータベース プロジェクトにあり、メイン プロジェクトで使用しようとしています。問題なく作成されますが、それを介してメソッドを呼び出すと、次のエラーが発生します。

System.NotSupportedException was unhandled by user code
HResult=-2146233067
Message=Unable to determine the provider name for connection of type 'MySql.Data.MySqlClient.MySqlConnection'.
Source=EntityFramework
StackTrace:
   at System.Data.Entity.ModelConfiguration.Utilities.DbConnectionExtensions.GetProviderInvariantName(DbConnection connection)
   at System.Data.Entity.Internal.InternalConnection.get_ProviderName()
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.Initialize()
   at System.Data.Entity.Internal.InternalContext.ExecuteSqlCommand(String sql, Object[] parameters)
   at System.Data.Entity.Database.ExecuteSqlCommand(String sql, Object[] parameters)
   at EPSCoR.Web.Database.Context.MySqlTableDbContext.DropTable(String table) in c:\Users\Devin\Documents\Visual Studio 2012\Projects\EPSCoR\Web\Database\Context\MySqlTableDbContext.cs:line 110
   at EPSCoR.Web.App.Repositories.Basic.BasicTableRepo.Drop(String tableName) in c:\Users\Devin\Documents\Visual Studio 2012\Projects\EPSCoR\Web\App\Repositories\Basic\BasicTableRepo.cs:line 71
   at EPSCoR.Web.App.Controllers.TableIndexController.Delete(Int32 id) in c:\Users\Devin\Documents\Visual Studio 2012\Projects\EPSCoR\Web\App\Controllers\ModelController.cs:line 172
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()

コンストラクターで DbContext に接続名 ("MySqlConnection") を渡すと、モデルを更新および作成できるため、接続文字列が機能することはわかっています。それが壊れる文字列のデータベースパラメータを変更しようとしたときだけです。また、これはすべてが1つのプロジェクトにあるときに機能したので、これが何らかの形で機能するはずであることを知っています.

ここに欠けているものはありますか?

MySql.Data v6.7.4.0 と EF v5 を nuget でインストールすると役立つ場合があります。

4

1 に答える 1