mysql - 如何在 mvc4 代码优先应用程序中使用 MySQL 成员资格?

标签 mysql ef-code-first asp.net-mvc-4 asp.net-membership membership-provider

我是使用 MySQL 的 MVC 新手。我在 MVC 4 中使用 MySQL 编写了代码优先应用程序。

public class Category
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }

    public virtual IEnumerable<Product> Products { get; set; }

}

public class Product
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public DateTime CreateDate { get; set; }
    public int CategoryID { get; set; }

    public virtual Category Category { get; set; }
}

public class MyContext : DbContext
{
    public DbSet<Product> Products { get; set; }
    public DbSet<Category> Categories { get; set; }
}

在 web.config 中:

  <connectionStrings>
    <add name="MyContext" connectionString="server=localhost;  User Id=root;  Pwd=mypass;  Persist Security Info=True;  database=catalogue" providerName="MySql.Data.MySqlClient " />
  </connectionStrings>

因此,这会在 mysql 中创建目录数据库。但我也想使用 mysql 成员资格。注册、创建用户并管理他们的角色。 例如,使用mvc4的默认AccountModels

我将此代码添加到 web.config:

<system.web>

    <membership defaultProvider="MySQLMembershipProvider">
          <providers>
            <clear />
            <remove name="MySQLMembershipProvider" />
            <add name="MySQLMembershipProvider" type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=6.5.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" applicationName="/" description="MySQL default application" connectionStringName="LocalMySqlServer" writeExceptionsToEventLog="True" autogenerateschema="True" enablePasswordRetrieval="True" enablePasswordReset="True" requiresQuestionAndAnswer="False" requiresUniqueEmail="False" passwordFormat="Clear" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression="" />
          </providers>
        </membership>

        <profile defaultProvider="MySQLProfileProvider">
          <providers>
            <clear />
            <remove name="MySQLProfileProvider" />
            <add name="MySQLProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider, MySql.Web, Version=6.5.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" applicationName="/" description="Profiles" connectionStringName="LocalMySqlServer" writeExceptionsToEventLog="False" autogenerateschema="True" />
          </providers>
        </profile>

        <roleManager enabled="false" defaultProvider="MySQLRoleProvider">
          <providers>
            <clear />
            <remove name="MySQLRoleProvider" />
            <add name="MySQLRoleProvider" type="MySql.Web.Security.MySQLRoleProvider, MySql.Web, Version=6.5.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" applicationName="/" description="Project Roles" connectionStringName="LocalMySqlServer" writeExceptionsToEventLog="True" autogenerateschema="True" />
          </providers>
        </roleManager>

  </system.web>

运行应用程序时出现此错误:

enter image description here

我忘记了哪一步?我无法使用成员(member)资格。

(抱歉英语不好)

最佳答案

如果您想要使用 VS2010 或 VS2012 的 MVC4 Internet 模板附带的成员(member)提供程序,以及 SQL Server 系列以外的数据库,即 SQL Server、SQL Compact 和 Azure,我认为这是不可行的,因为 MVC4 使用 SimpleMembership 提供程序,它与数据库无关。

话虽如此,但这并不意味着您无法实现您想要的目标。我建议使用 Microsoft ASP.NET Universal Providers Core Libraries。我建议您阅读这篇优秀的Post

在这篇文章中,作者 Scott Hanselman 提到了以下内容

Using these Universal "Default Profile Providers" means all you have to do is set the right connection string and your applications that use these services will work with SQL Server (plus Express), SQL Server Compact and SQL Azure with no code changes from you.

嗯,其他 SQL 标准怎么样?我们按照以下步骤操作,

1) 确保首先更改连接字符串,然后使用 Nuget Package Manager 安装 MySQL.Web 和 MySQL.Data。

    <connectionStrings>
      <add name="MySQLConn" connectionString="Server=localhost;Database=dbname;Uid=dbuser;Pwd=dbpass;" />
    </connectionStrings>

2) 使用 Nuget 包管理器查找并安装“Microsoft ASP.NET Universal Providers Core Libraries”或在包管理器控制台中运行以下命令,

Install-Package system.web.providers
Install-Package Microsoft.AspNet.Providers.Core

3) 转到“项目菜单”->“ASP.Net 配置”,您将在浏览器中看到您的 Web 项目的 ASP Net 管理工具。

4) 然后转到“提供商”选项卡,转到“为每个功能选择不同的提供商(高级)”

5) 确保选择 MySQL Membership Provider。

6) 然后转到“安全”选项卡,并创建一些您认为项目必需的角色和用户。 如果由于某种原因您在此处收到错误,请尝试更改您的网络配置,并将成员身份、角色和个人资料替换为 this

7)享受吧!

希望这有帮助

PS:我对此也很陌生,因此我愿意接受有关针对其他 SQL 数据库的 MVC4 最佳成员(member)提供程序的建议。

关于mysql - 如何在 mvc4 代码优先应用程序中使用 MySQL 成员资格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13054956/

相关文章:

javascript - 通过 Express、Axios 在 MySQL PUT 和 DELETE 上出现错误 500

mysql - sql 错误 SyntaxException 准备好的语句

windows-services - TopShelf、Ninject 和 EF 代码优先的 Ninject 范围问题

asp.net - 在 Asp.net mvc4 应用程序中向 webgrid 添加垂直滚动

javascript - 将 javascript 对象数组发送到 MVC4 Controller

mysql - 无法启动 mysqld

entity-framework - EF4 CTP5 : DbContext inheritance

c# - 无法创建级联外键

jquery - 如何选择在mvc中动态选择的css类

php - 动态创建复选框