1.創(chuàng)建 ASP.NET Web APP


2.替換默認(rèn)的數(shù)據(jù)庫鏈接
在 Web.config 文件的 connectionStrings 配置節(jié)中替換默認(rèn)的鏈接字符串為:
<add name="DefaultConnection" connectionString="Server=localhost;Port=13306;Database=databasename;User id=uid;Password=somepass;" providerName="MySql.Data.MySqlClient" />
3.創(chuàng)建默認(rèn)數(shù)據(jù)表
創(chuàng)建表的SQL腳本
更改數(shù)據(jù)表區(qū)分大小寫
在[mysqld]節(jié)點(diǎn)下,加入一行:lower_case_table_names=1

4.刪除默認(rèn)的 Microsoft.AspNet.Identity.EntityFramework 包
使用命令行: Uninstall-Package Microsoft.AspNet.Identity.EntityFramework
或
直接使用安裝界面點(diǎn)擊刪除

如有依賴包需要先刪除,如 Microsoft.AspNet.Identity.EntityFramework.zh-Hans
5.安裝 MySql.AspNet.Identity 包
命令行: Install-Package MySql.AspNet.Identity -Version 2.1.1
Dependencies
- Microsoft.AspNet.Identity.Core (>= 2.1.0)
- MySql.Data(>= 6.9.5 && < 7.0.0)
6.替換代碼
1.替換引用包
在 ~/Models/IdentityModels.cs (以及其他引用到這兩個(gè)包的)文件中刪除以下兩個(gè)引用

using System.Data.Entity;
using Microsoft.AspNet.Identity.EntityFramework;
替換為
using MySql.AspNet.Identity;
2.刪除不必要代碼
在 ~/Models/IdentityModels.cs 中刪除這個(gè)類
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
在 ~/App_Start/Startup.Auth.cs 中刪除這行代碼
app.CreatePerOwinContext(ApplicationDbContext.Create);
3.刪除引用替換代碼
在 ~/App_Start/IdentityConfig.cs 文件中刪除以下引用
using System.Data.Entity;
using Microsoft.AspNet.Identity.EntityFramework;
添加以下引用
using MySql.AspNet.Identity;
替換代碼
// 替換掉被注釋的這行代碼
// var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
var manager = new ApplicationUserManager(new MySqlUserStore<ApplicationUser>());
7.運(yùn)行后功能可正常使用
項(xiàng)目代碼見GitHub - Identity2MySql