2013-08-20 45 views
0

我正在使用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。

回答

0

我不知道为什么这个工作,但我重新安装了EntityFramework和MySql.Data包,然后一切又开始工作。

相关问题