我正在使用ASP.NET MVC4,並試圖將我的數據訪問層移至單獨的項目。我網站上的每個用戶都有自己的數據庫,因此我需要爲每個用戶在連接字符串中指定一個不同的數據庫。我目前這樣做的方法是:Gettting無法確定與MySql連接的提供程序名稱
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」)可以更新和創建模型。它只有當我嘗試更改它打破的字符串中的數據庫參數。當一切都在一個項目中時,這也是有效的,所以我知道這應該以某種方式工作。
有什麼我在這裏失蹤?
萬一它可能會幫助我通過nuget安裝MySql.Data v6.7.4.0和EF v5。