mysql - 无法确定与 MySql 连接的提供者名称

标签 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 有所帮助。

最佳答案

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

关于mysql - 无法确定与 MySql 连接的提供者名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18344185/

相关文章:

mysql - 仅当列为空时才更新列——在replace into语句中

c# - 使用用户名将对象添加到数据库

javascript - 在提交到服务器 MVC 之前创建一个预览页面

c++ - 谁拥有 MySQL Connector C++ 返回的内存?

visual-studio-2017 - 可以在 vs 2017 上构建 Mysql Connector C++ 吗?

mysql - 无法在同一 Docker 容器中从 Python 访问 MySQL 数据库

mysql - 获取sql中的行号

php - 如果字段匹配则更新和删除

php - 如何创建从 nodejs mysql 模块获取的正确的 json 类型数据?

asp.net-mvc-4 - 使用 AppPool 的自定义标识时,StaticFile 上出现 HTTP 错误 500.0