100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > .NET Core Identity 使用Mysql数据库

.NET Core Identity 使用Mysql数据库

时间:2023-04-04 16:27:12

相关推荐

.NET Core Identity 使用Mysql数据库

一、在NuGet安装EF

1、 Pomelo.EntityFrameworkCore.MySql

2、Pomelo.EntityFrameworkCore.MySql.Design

3、Microsoft.EntityFrameworkCore.Tools

二、新建identity用户角色实体类和数据库上下文

用户类和角色类

public class ApplicationUser : IdentityUser{//扩展用户属性}public class ApplicationRole : IdentityRole{//扩展角色属性}

数据库上下文

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string>{public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options){}protected override void OnModelCreating(ModelBuilder builder){base.OnModelCreating(builder);}}

三、在appsettings.json文件中配置数据库连接字符串

"ConnectionStrings": {"DefaultConnection": "Server=127.0.0.1;database=testdb;uid=root;password=pwd123456;TreatTinyAsBoolean=true"},

注意TreatTinyAsBoolean=true 最好加上 原因:bool类型字段对应到ef会生产bit字段。如果不在连接字符串中添加TreatTinyAsBoolean=true 插入数据的时候会报Unable to cast object of type 'System.Boolean' to type 'System.Int16这个错

四、在Startup文件中注册数据库上下文服务和对identity进行相关设置

注册服务

services.AddDbContext<ApplicationDbContext>(options =>options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

services.AddDbContext<ApplicationDbContext>(options => options.UseMySql(Configuration.GetConnectionString("DefaultConnection")));services.AddIdentity<ApplicationUser, ApplicationRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();

对identity进行配置(可不做!)

services.Configure<IdentityOptions>(options =>{// Password settingsoptions.Password.RequireDigit = false;options.Password.RequiredLength = 6;options.Password.RequireNonAlphanumeric = false;options.Password.RequireUppercase = false;options.Password.RequireLowercase = false;options.Password.RequiredUniqueChars = 1;// Lockout settingsoptions.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);options.Lockout.MaxFailedAccessAttempts = 10;options.Lockout.AllowedForNewUsers = true;// User settingsoptions.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";options.User.RequireUniqueEmail = false;});services.ConfigureApplicationCookie(options =>{// Cookie settingsoptions.Cookie.HttpOnly = true;options.Cookie.Expiration = TimeSpan.FromDays(150);// If the LoginPath isn't set, Core defaults // the path to /Account/Login.options.LoginPath = "/Account/Login";// If the AccessDeniedPath isn't set, Core defaults // the path to /Account/AccessDenied.options.AccessDeniedPath = "/Account/AccessDenied";options.SlidingExpiration = true;});

五、迁移(在程序包管理控制台中)

1、输入Enable-Migrations 开启迁移

2、输入Add-Migration Initial建立快照(Initial快照名称随意写)

成功之后生成迁移文件

3、输入 Update-Database 升级数据库

成功之后查看数据库生成情况

新增数据表

一、添加实体类TestTable

public class TestTable{public int TestTableId { get; set; }public string TestTableName { get; set; }}

在数据库上下文中添加表映射

public DbSet<TestTable> TestTable { get; set; }

Add-Migration Initial1执行迁移

Update-Database 升级数据库

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。