无法确定与MySql连接的提供程序名称

时间:2013-08-20 20:13:32

标签: mysql asp.net-mvc-4 mysql-connector

我正在使用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可能有帮助。

1 个答案:

答案 0 :(得分:0)

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