You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Visual Studio 2017中配置Identity连接MySQL并创建身份表?

在Visual Studio 2017中配置ASP.NET Identity使用MySQL数据库

我来一步步帮你搞定VS2017里ASP.NET Identity对接MySQL的配置,亲测有效:

1. 前期准备

  • 确保你的机器上已经安装了MySQL Server(建议5.7或8.0版本,兼容性更好)
  • 安装MySQL Connector/NET(对应.NET Framework版本,比如8.0.x系列,安装时记得勾选「Visual Studio Integration」选项,让VS能识别MySQL数据源)

2. 安装必要的NuGet包

打开你的ASP.NET项目,右键选择「管理NuGet程序包」,搜索并安装以下包:

  • MySql.Data.EntityFramework(版本要和你安装的Connector/NET匹配,比如8.0.28)
  • 如果项目还没引入Identity相关包,同时安装Microsoft.AspNet.Identity.EntityFramework和Microsoft.AspNet.Identity.Core

也可以直接在「包管理器控制台」执行命令:

Install-Package MySql.Data.EntityFramework -Version 8.0.28
Install-Package Microsoft.AspNet.Identity.EntityFramework -Version 2.2.3

3. 修改数据库连接字符串

找到项目根目录下的Web.config,替换原有的SQL Server连接字符串为MySQL版本:

<connectionStrings>
    <add name="DefaultConnection" 
         connectionString="server=localhost;port=3306;database=你的数据库名;uid=你的MySQL用户名;pwd=你的MySQL密码;" 
         providerName="MySql.Data.MySqlClient" />
</connectionStrings>

注意:providerName必须指定为MySql.Data.MySqlClient,不能用默认的SQL Server provider。

4. 配置Identity的DbContext

找到项目中的ApplicationDbContext类(通常在Models文件夹下),修改它的实现以适配MySQL:

using Microsoft.AspNet.Identity.EntityFramework;
using System.Data.Entity;

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
        // 设置MySQL数据库初始化策略,这里用「不存在则创建」
        Database.SetInitializer(new CreateDatabaseIfNotExists<ApplicationDbContext>());
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        
        // 可选:自定义Identity表名(默认是AspNetUsers、AspNetRoles等,这里改成更简洁的名称)
        modelBuilder.Entity<IdentityUser>().ToTable("Users");
        modelBuilder.Entity<IdentityRole>().ToTable("Roles");
        modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins");
    }
}

同时在Web.config的entityFramework节点下添加MySQL的provider配置:

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
        <parameters>
            <parameter value="mssqllocaldb" />
        </parameters>
    </defaultConnectionFactory>
    <providers>
        <!-- 添加MySQL provider -->
        <provider invariantName="MySql.Data.MySqlClient" 
                  type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.EntityFramework" />
    </providers>
</entityFramework>

5. 执行EF迁移生成Identity表

打开「包管理器控制台」(路径:工具 → NuGet包管理器 → 包管理器控制台),依次执行以下命令:

  1. 启用迁移(如果项目还没启用过):
Enable-Migrations
  1. 创建初始迁移:
Add-Migration InitialIdentitySetup
  1. 应用迁移到MySQL数据库:
Update-Database

执行完成后,打开MySQL客户端(比如Navicat、MySQL Workbench),就能看到生成的Identity相关表了。

额外注意事项

  • 版本兼容性:确保MySql.Data.EntityFramework的版本和你安装的MySQL Connector/NET、EntityFramework版本匹配,比如EF6.4.x搭配MySQL Connector/NET 8.0.x是比较稳定的组合
  • 如果是ASP.NET Core项目(VS2017也支持Core),则需要安装Pomelo.EntityFrameworkCore.MySql包,配置方式略有不同,核心思路是替换EF Core的provider并修改连接字符串
  • 连接字符串中的特殊字符:如果密码包含&、@等特殊字符,需要进行URL编码

内容的提问来源于stack exchange,提问作者user3293752

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 11:12:41