1

在我的第一个 MVC 4 应用程序中,我在代码中遇到以下错误。

WebSecurity.InitializeDatabaseConnection("fousuEntities", "usertable", "UserID", "UserCode", autoCreateTables: true);

在我的配置文件中,我确实有以下条目,

<add name="fousuEntities" connectionString="metadata=res://*/fousutable.csdl|res://*/fousutable.ssdl|res://*/fousutable.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost;User Id=user;password=pwd;database=fousutable&quot;" providerName="System.Data.EntityClient" />

请帮助我在这段代码中遗漏了什么?

  • 视觉工作室 2010 SP1
  • MVC 4
  • 尝试使用 simplemembership 提供程序
  • 已安装的实体框架 5.0
  • .Net 框架 4.0
  • 数据库 MySQL 5.1

更新 1:

错误详情 :

找不到请求的 .Net Framework 数据提供程序。它可能没有安装。

说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.ArgumentException:找不到请求的 .Net Framework 数据提供程序。它可能没有安装。

Line 32:                     using (var context = new UsersContext())
Line 33:                     {
Line 34:                         if (!context.Database.Exists())
Line 35:                         {
Line 36:                             // Create the SimpleMembership database without      Entity Framework migration schema

更新 2:

好吧,我可以通过更改 web.config 中的条目来摆脱上述错误。如下,虽然最终得到另一个错误。

<add name="fousuEntities" connectionString="Server=MySQL; Database=fousutable; uid=user; pwd=pwd;" providerName="MySql.Data.MySqlClient"/>

并在 System.Data 下添加了一个条目,

<system.data>
<DbProviderFactories>
  <remove invariant="MySql.Data.MySqlClient" />
  <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=5.1.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>

现在我得到的新错误是,

调用的目标已引发异常。对于 LazyInitializer 行,

public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // Ensure ASP.NET Simple Membership is initialized only once per app start
        LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
    }

在参考了InnerException的详细信息后,我得到了“无法初始化 ASP.NET 简单成员资格数据库”。

4

1 回答 1

0

Check the name of your connection string. Most probably you have changed from "DefaultConnection" to any other value and sure you have changed it properly in the SimpleMembershipInitializer in the line

WebSecurity.InitializeDatabaseConnection("MyNewConnectionString", "Users", "UserId", "UserName", autoCreateTables: true);

But you have to be aware that in your AccountModels.cs file you have a UsersContext and you have to update that in the constructor:

    public UsersContext() : base("MyNewConnectionString")
    {
    }

Otherwise it will continue to try to find "DefaultConnection" which doesn´t exist. That could be the reason why it fails.

于 2013-11-26T20:23:35.567 回答