Migragtion的命令,左边是手动命令,右边是代码方式

首先来看命令方式:

创建一个mvc项目,默认已经集成了EF包

创建的项目包含了Microsoft.AspNetCore.Identity.EntityFramewordCore包,这将使用Entity Framework Core通过SQL Server来储存身份识别的数据和表信息。

添加Sql连接字符串:

{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"conn": "Data Source=pc;User ID=sa;Password=12;database=CoreDb;Connect Timeout=30;Encrypt=False;
TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
}
}

注意这个:ConnectionStrings 是必须要这样写的,默认是根据这个名字读取,后面会说

Net Core 身份 Identity 允许向应用中添加登陆功能。用户可创建一个账户并进行登陆,登陆时可使用用户名、密码

Server数据库存储用户名字、密码和配置文件数据。另外,你可使用其他已有的存储空间存储数据

创建ApplicationUser, ApplicationRole 分别继承IdentityRole IdentityUser

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity; namespace IdentityDemo.Models
{
public class ApplicationRole:IdentityRole
{
}
}
using Microsoft.AspNetCore.Identity;

namespace IdentityDemo.Models
{
public class ApplicationUser : IdentityUser
{
}
}

创建上下文类:

using IdentityDemo.Models;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; namespace IdentityDemo.Date
{ public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
}
}

ConfigureServices 中注册服务

public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
}); services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Login";
}); services.AddDbContext<ApplicationDbContext>(options =>
{
//配置文件前面必须是;ConnectionStrings
//因为默认是:GetSection("ConnectionStrings")[name].
options.UseSqlServer(Configuration.GetConnectionString("conn"));
//options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
}); services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders(); //配置密码注册方式
services.Configure<IdentityOptions>(options =>
{
options.Password.RequireLowercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequiredLength = ;
}); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

我这里用了Cookie验证,所以应该添加中间件  app.UseAuthentication();

创建用户:通过依赖注入: UserManager 和 SignInManager 依赖:Microsoft.AspNetCore.Identity;

 private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signinManager;
public LoginController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signinManager)
{
_userManager = userManager;
_signinManager = signinManager;
}

注册代码

[HttpPost]
public async Task<IActionResult> Registe(LoginViewModel model)
{
var identityUser = new ApplicationUser
{
UserName = model.UserName,
PasswordHash = model.PassWorld,
NormalizedEmail = model.UserName
}; var identityResult = await _userManager.CreateAsync(identityUser, model.PassWorld);
if (identityResult.Succeeded)
{
return RedirectToAction("Index", "Home");
}
return View();
}

注册之前,我们先通过命令生成数据库

命令依赖于:Microsoft.EntityFrameworkCore.Tools包,不过默认也有了

可以输入dotnet ef migrations --help 看看帮助

输入后命令:dotnet ef migrations add identity

项目中会生成一个Migrations的文件夹和文件

然后更新数据库:dotnet ef migrations update

可以看到生成了数据库和表,但AspNetUsers表的组建ID默认是字符串类型的,一连串GUID,待会会说改成常用的int类型

好了。现在注册一个看看

看看默认的:

接下来我们修改默认的主键为int类型。只需要改成:IdentityUser<int> ,IdentityRole<int>

上下文改成:

因为是重新生成数据库,需要删除数据库,并且把Migrations的文件都删除,或者直接删除Migrations文件夹

可以执行命令删除库:dotnet ef database drop

否则EF会当作升级和降级处理,不会重新生成数据库

然后重新migragins add ,update 注册看看,已经是int类型了

然后给表升级,在用户表添加一列 TrueName,首先看用户名表。默认是没有该列的

我们在ApplicationUser类新增字段

然后命令:dotnet ef migrations add AddTrueName  //AddTrueName名字是任意取的

此时在Migrations文件夹生成了对呀的类

然后update,这里的update只会找最新的migrations 这里的最新的migrations就是20190126061435_identity

数据库已经有了

比如我们在添加一个Address列,然后在还原到只有TrueName的状态

现在Migrations有3个Migration

然后我们通过update回滚

dotnet ef database update 回滚的migration

我们这里回滚到AddTrueName 那么命令就是:dotnet ef database update AddTrueName

回滚之后。表也更新了

其实__EFMigrationsHistory表是保存了当前所有的Migration

回滚前

回滚后

这里有两个AddTrueName是因为我add了两次,可以不用管

主要是对比之前和之后,AddAddress没有了

然后看:dotnet ef migrations script 生成脚本

生成脚本在屏幕中,还得拷贝出来,多不友好,看看--help,是可以指定路径的

执行: dotnet ef migrations script -o d:\script\user.sql

打开生成的文件看看

dotnet ef migratoins remove 是移除最新的一个migration,remove一次就移除一次,

前提是该migration没有update到database,如果细心你会发现,当你add migration时候,会提示,可以remove 移除

我们添加3个 migration 分别是 temp01,temp02,temp03

此时已经添加了3个测试的

记住,我这里都没有执行update的,所以在__EFMigrationsHistory也不会记录。该表记录的是已经update到database的migration

我们执行一次命令:dotnet ef migrations remove

因为temp03是最后添加的,所以先删除,是不是像入栈出栈

在remove一次,按照上面说的,应该是移除temp02

就不一一测试了

来看看,如果update 后。能不能remove,上面已经还原到了TrueName

我们先升级到Address

执行命令:dotnet ef database update Addaddress

表字段和历史纪录也都有了

然后我们执行命令:dotnet ef migrations remove

这里要注意一点,我们是加了Adress后,我为了测试,又加了temp01,02,03,上面测试只remove了03,02

如果你没有remove01的话,现在remove是remove的01,注意下就好了

现在我们默认01,02,03都已经remove了。最新的肯定就是address了

好了,有点啰嗦。执行命令:dotnet ef migrations remove

info提示该migratin已经udate到database

通过dotnet ef migrations remove --help

可以看到有其他选项,有兴趣的可以自行研究

上面都是手动执行命令,我们来看看在代码里面怎么做

在程序初始化的时候,添加一个默认管理员的账号,实例代码如下

创建一个ApplicationDbContextSeed类,用于创建默认用户

using IdentityDemo.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging; namespace IdentityDemo.Date
{
public class ApplicationDbContextSeed
{
private UserManager<ApplicationUser> _userManger; /// <summary>
///
/// </summary>
/// <param name="context">上下文</param>
/// <param name="server">依赖注入的容器,这里可以获取依赖注入</param>
/// <returns></returns>
public async Task AsyncSpeed(ApplicationDbContext context, IServiceProvider server)
{
try
{
_userManger = server.GetRequiredService<UserManager<ApplicationUser>>();
var logger = server.GetRequiredService<ILogger<ApplicationDbContext>>();
logger.LogInformation("speed Init"); //如果没有用户,则创建一个
if (!_userManger.Users.Any())
{
var defaultUser = new ApplicationUser
{
UserName = "Admin",
dEmail = "cnblogs@163.com"
};
var userResult = await _userManger.CreateAsync(defaultUser, "");
if(!userResult.Succeeded)
{
logger.LogError("创建失败");
//logger.LogInformation("初始化用户失败");
userResult.Errors.ToList().ForEach(e => {
logger.LogError(e.Description);
});
}
}
}
catch (Exception ex)
{
throw new Exception("初始化用户失败");
}
}
}
}

添加一个对IWebHost扩展的类:

using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
namespace IdentityDemo.Date
{
public static class WebHostMigrationExtensions
{
public static IWebHost MigrationDbContext<TContext>(this IWebHost host, Action<TContext, IServiceProvider> sedder) where TContext : DbContext
{
using (var scope = host.Services.CreateScope())
{
//拿到依赖注入容器
var servers = scope.ServiceProvider;
//var logger = servers.GetRequiredService<ILogger<TContext>>();
var logger = servers.GetService<ILogger<TContext>>();
var context = servers.GetService<TContext>(); try
{
context.Database.Migrate();
sedder(context, servers);
logger.LogInformation($"执行DbContex{typeof(TContext).Name}seed方法成功");
}
catch (Exception ex)
{
logger.LogError(ex, $"执行DbContex{typeof(TContext).Name}seed方法失败");
}
}
return host;
}
}
}

这样就可以在Program.cs的main方法中调用

因为方法是异步方法,所以用Wait()方法改为同步的

然后删除Migration中的所有文件,重新add,可以 不update,因为代码会:context.Database.Migrate();

然后数据库已经初始化了

通过var user =  _userManager.FindByEmailAsync("cnblogs@163.com").Result;可以查找用户,

 [HttpPost]
public IActionResult Login(LoginViewModel model)
{
var user = _userManager.FindByEmailAsync(model.Email).Result; if (user!=null)
{
var claims = new List<Claim> {
new Claim("name",model.UserName)
};
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); var cliamsPrincipal = new ClaimsPrincipal(claimsIdentity); HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, cliamsPrincipal); return RedirectToAction("Index", "Home");
}
return RedirectToAction("Index");
}

本文主要讲了magration的操作和identity身份的验证

源码:https://github.com/byniqing/AspNetCore-identity

参考资料

https://docs.microsoft.com/en-us/aspnet/core/migration/proper-to-2x/?view=aspnetcore-2.2

https://www.cnblogs.com/jqdy/p/5941248.html

https://docs.microsoft.com/zh-cn/ef/core/miscellaneous/cli/dotnet#dotnet-ef-migrations-remove

Asp.net Core Mvc EF- Migrations使用的更多相关文章

  1. ASP.NET Core MVC+EF Core从开发到部署

    笔记本电脑装了双系统(Windows 10和Ubuntu16.04)快半年了,平时有时间就喜欢切换到Ubuntu系统下耍耍Linux,熟悉熟悉Linux命令.Shell脚本以及Linux下的各种应用的 ...

  2. ASP.NET Core MVC+EF Core项目实战

    项目背景 本项目参考于<Pro Entity Framework Core 2 for ASP.NET Core MVC>一书,项目内容为party邀请答复. 新建项目 本项目开发工具为V ...

  3. ASP.NET Core MVC+Layui使用EF Core连接MySQL执行简单的CRUD操作

    前言: 本章主要通过一个完整的示例讲解ASP.NET Core MVC+EF Core对MySQL数据库进行简单的CRUD操作,希望能够为刚入门.NET Core的小伙伴们提供一个完整的参考实例.关于 ...

  4. 使用EF Core+CodeFirst建立ASP.NET Core MVC项目

    本篇随笔介绍如何使用.NET Core+EF Core创建Web应用程序 首先借用官网的话简单介绍一下ASP.NET Core ASP.NET Core 是一个跨平台的高性能开源框架,用于生成基于云且 ...

  5. 创建ASP.NET Core MVC应用程序(5)-添加查询功能 & 新字段

    创建ASP.NET Core MVC应用程序(5)-添加查询功能 & 新字段 添加查询功能 本文将实现通过Name查询用户信息. 首先更新GetAll方法以启用查询: public async ...

  6. 创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表

    创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表 创建数据模型类(POCO类) 在Models文件夹下添 ...

  7. Working with Data » 使用Visual Studio开发ASP.NET Core MVC and Entity Framework Core初学者教程

    原文地址:https://docs.asp.net/en/latest/data/ef-mvc/intro.html The Contoso University sample web applica ...

  8. 008.Adding a model to an ASP.NET Core MVC app --【在 asp.net core mvc 中添加一个model (模型)】

    Adding a model to an ASP.NET Core MVC app在 asp.net core mvc 中添加一个model (模型)2017-3-30 8 分钟阅读时长 本文内容1. ...

  9. 使用ASP.NET Core MVC 和 Entity Framework Core 开发一个CRUD(增删改查)的应用程序

    使用ASP.NET Core MVC 和 Entity Framework Core 开发一个CRUD(增删改查)的应用程序 不定时更新翻译系列,此系列更新毫无时间规律,文笔菜翻译菜求各位看官老爷们轻 ...

  10. 如何使用ASP.NET Core、EF Core、ABP(ASP.NET Boilerplate)创建分层的Web应用程序(第一部分)

    本文是为了学习ABP的使用,是翻译ABP官方文档的一篇实战教程,我暂时是优先翻译自己感兴趣或者比较想学习的部分,后续有时间希望能将ABP系列翻译出来,除了自己能学习外,有可能的话希望帮助一些英文阅读能 ...

随机推荐

  1. 【资料下载区】【iCore、 iCore2相关资料】更新日期2017/1/11

    [新产品发布][iCore2 ARM / FPGA 双核心板] [iCore系列核心板视频教程]之 SDRAM 读写实验[iCore双核心组合是开发板例程][uCGUI 例程及代码包下载][iCore ...

  2. 【iCore4 双核心板_FPGA】实验十九:使用JTAT UART终端打印信息

    实验指导书及源代码下载地址: 链接:https://pan.baidu.com/s/1c3mqDkW 密码:4x9h iCore4链接:

  3. opcache分享

    opcache的技术分享ppt,独家唯一经过自己实验的分享 https://pan.baidu.com/s/1-73-QHOSeet7tcR81gto6Q

  4. Linux服务器重启后MySQL启动失败

    Redirecting to /bin/systemctl start  mysql.service Job for mysqld.service failed because the control ...

  5. C++ 智能指针七

    /* 智能指针weak_ptr */ #include <iostream> #include <string> #include <memory> /* weak ...

  6. 使用.gitignore删除Github上的.idea文件

    环境:windows + git bash. 一.问题来源 由于之前用Goland建立Golang工程时,生成了.idea文件,不小心上传至Github: 所以尝试用.gitignore进行忽略不上传 ...

  7. laravel5.8笔记三:常用命令

    创建控制器 php artisan make:controller Index/IndexController 创建模型 php artisan make:model Index/IndexContr ...

  8. mysql可以远程连接的配置

    由于配置好几次了,老是会忘记命令,所以记录下来 1.修改配置文件 我的配置文件是/etc/mysql/mysql.conf.d/mysqld.cnf 找到 bind-address = 127.0.0 ...

  9. nodejs在windows下的安装配置(使用NVM的方式)

    NVM的安装 1.下载安装包,https://github.com/coreybutler/nvm-windows/releases 2.下载完成后点击nvm-setup,按步骤安装,注意路径中不能带 ...

  10. 23命令模式Command

    一.什么是命令模式 Command模式也叫命令模式 ,是行为设计模 式的一种.Command模式通过被称为 Command的类封装了对目标对象的调用行为以及调用参数. 二.命令模式的应用场景 在面向对 ...