我正在使用 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”)时,我可以更新和创建模型。只有当我尝试更改它破坏的字符串中的数据库参数时。当一切都在一个项目中时,这也有效,所以我知道这应该以某种方式工作。
我在这里有什么遗漏吗?
如果它可能会帮助我通过 nuget 安装 MySql.Data v6.7.4.0 和 EF v5。