Individual authentication 模板

我们首先用VSCode新建一个mvc的网站,这个网站创立的时候回自动为我们创建Identuty Core以及EF Core的代码示例,我们可以用命令    dotnet new mvc --help    来查看一些参数:

由于我们创建mvc项目是默认不带Identity验证的,所以我们要加上下面的  -au|--auth  参数来使用Individual创建带Identity验证的网站

还有一个参数  -uld|--use-local-db   参数来使用本地数据库,在VSCode默认的是使用SqlLite,在VS2017中默认使用的是LocalDB。

接下来我们就可以使用以下命令创建包含Identity的mvc网站IdentitySample

dotnet new mvc -au Individual -uld --name IdentitySample

创建完成后我们打开创建的项目,然后查看appsettings.json,发现已经默认创建了数据库连接,我们可以将数据库修改成自己的数据库地址

同时我们可以打开Startup.cs可以查看数据库配置以及Identity验证已经帮我们添加好了

接下来我们初始化一下数据库,否则启动之后会报错,所以我们要使用EF Core的Migration命令来初始化数据库。

我们使用的第一个命令是  dotnet ef database update  他会根据当前migration文件夹下的文件来帮我们进行数据库的创建和更新。

我们可以在终端看到执行的sql语句

 

然后我们去数据库就可以查看到我们创建的数据库和数据表了

接下来我们运行  dotnet run  来运行我们创建的mvc网站,发现已经实现了登录和注册,以及前后台验证

EF Core Migration

手动命令行的方式

VSCode VS2017 说明
dotnet ef migrations add InitialCreate Add-Migration  对当前EF实体模型增加一个配置文件
dotnet ef database update Update-Database  对当前版本进行更新
dotnet ef migrations remove Remove-Migration  删除最新的Migration
dotnet ef database update LastGoodMigration Update-Database LastGoodMigration  对指定版本进行更新
dotnet ef migrations script Script-Migration  对当前更新生成一个sql的脚本,我们可以使用脚本到数据库取执行

接下来我们实践一下,我们来操作一下数据库中的AspNetUsers表,我们打开项目中的ApplicationUser.cs,添加新属性NewColumn

然后我们使用命令  dotnet ef migrations add AddNewColumn  生成配置文件

这个时候数据库是没有进行更新的,我们只有执行  dotnet ef database update  命令才会更新到数据库

接下来我们继续打开项目中的ApplicationUser.cs,添加新属性Address

然后我们使用命令  dotnet ef migrations add AddAddress  生成配置文件

执行  dotnet ef database update  命令更新到数据库

接下来我们使用  dotnet ef database update AddNewColumn  就可以将数据库回滚到指定的版本

接下来执行  dotnet ef migrations remove  命令会将当前项目之后没有用的配置文件删除,我们这里执行后会将AddAddress配置文件删除

PS C:\Users\Administrator\Desktop\Demo2\IdentitySample> dotnet ef migrations remove
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[]
User profile is available. Using 'C:\Users\Administrator\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
info: Microsoft.EntityFrameworkCore.Infrastructure[]
Entity Framework Core 2.0.-rtm- initialized 'ApplicationDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
info: Microsoft.EntityFrameworkCore.Database.Command[]
Executed DbCommand (8ms) [Parameters=[], CommandType='Text', CommandTimeout='']
SELECT OBJECT_ID(N'__EFMigrationsHistory');
info: Microsoft.EntityFrameworkCore.Database.Command[]
Executed DbCommand (8ms) [Parameters=[], CommandType='Text', CommandTimeout='']
SELECT [MigrationId], [ProductVersion]
FROM [__EFMigrationsHistory]
ORDER BY [MigrationId];
Removing migration '20180105053249_AddAddress'.
Reverting model snapshot.
Done.

我们可以用  dotnet ef migrations script  命令来生成sql脚本,我们可以将sql拷贝出来放在数据库取执行。

IF OBJECT_ID(N'__EFMigrationsHistory') IS NULL
BEGIN
CREATE TABLE [__EFMigrationsHistory] (
[MigrationId] nvarchar(150) NOT NULL,
[ProductVersion] nvarchar(32) NOT NULL,
CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
);
END;

GO

CREATE TABLE [AspNetRoles] (
[Id] nvarchar(450) NOT NULL,
[ConcurrencyStamp] nvarchar(max) NULL,
[Name] nvarchar(256) NULL,
[NormalizedName] nvarchar(256) NULL,
CONSTRAINT [PK_AspNetRoles] PRIMARY KEY ([Id])
);

GO

CREATE TABLE [AspNetUserTokens] (
[UserId] nvarchar(450) NOT NULL,
[LoginProvider] nvarchar(450) NOT NULL,
[Name] nvarchar(450) NOT NULL,
[Value] nvarchar(max) NULL,
CONSTRAINT [PK_AspNetUserTokens] PRIMARY KEY ([UserId], [LoginProvider], [Name])
);

GO

CREATE TABLE [AspNetUsers] (
[Id] nvarchar(450) NOT NULL,
[AccessFailedCount] int NOT NULL,
[ConcurrencyStamp] nvarchar(max) NULL,
[Email] nvarchar(256) NULL,
[EmailConfirmed] bit NOT NULL,
[LockoutEnabled] bit NOT NULL,
[LockoutEnd] datetimeoffset NULL,
[NormalizedEmail] nvarchar(256) NULL,
[NormalizedUserName] nvarchar(256) NULL,
[PasswordHash] nvarchar(max) NULL,
[PhoneNumber] nvarchar(max) NULL,
[PhoneNumberConfirmed] bit NOT NULL,
[SecurityStamp] nvarchar(max) NULL,
[TwoFactorEnabled] bit NOT NULL,
[UserName] nvarchar(256) NULL,
CONSTRAINT [PK_AspNetUsers] PRIMARY KEY ([Id])
);

GO

CREATE TABLE [AspNetRoleClaims] (
[Id] int NOT NULL IDENTITY,
[ClaimType] nvarchar(max) NULL,
[ClaimValue] nvarchar(max) NULL,
[RoleId] nvarchar(450) NOT NULL,
CONSTRAINT [PK_AspNetRoleClaims] PRIMARY KEY ([Id]),
CONSTRAINT [FK_AspNetRoleClaims_AspNetRoles_RoleId] FOREIGN KEY ([RoleId]) REFERENCES [AspNetRoles] ([Id]) ON DELETE CASCADE
);

GO

CREATE TABLE [AspNetUserClaims] (
[Id] int NOT NULL IDENTITY,
[ClaimType] nvarchar(max) NULL,
[ClaimValue] nvarchar(max) NULL,
[UserId] nvarchar(450) NOT NULL,
CONSTRAINT [PK_AspNetUserClaims] PRIMARY KEY ([Id]),
CONSTRAINT [FK_AspNetUserClaims_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id]) ON DELETE CASCADE
);

GO

CREATE TABLE [AspNetUserLogins] (
[LoginProvider] nvarchar(450) NOT NULL,
[ProviderKey] nvarchar(450) NOT NULL,
[ProviderDisplayName] nvarchar(max) NULL,
[UserId] nvarchar(450) NOT NULL,
CONSTRAINT [PK_AspNetUserLogins] PRIMARY KEY ([LoginProvider], [ProviderKey]),
CONSTRAINT [FK_AspNetUserLogins_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id]) ON DELETE CASCADE
);

GO

CREATE TABLE [AspNetUserRoles] (
[UserId] nvarchar(450) NOT NULL,
[RoleId] nvarchar(450) NOT NULL,
CONSTRAINT [PK_AspNetUserRoles] PRIMARY KEY ([UserId], [RoleId]),
CONSTRAINT [FK_AspNetUserRoles_AspNetRoles_RoleId] FOREIGN KEY ([RoleId]) REFERENCES [AspNetRoles] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_AspNetUserRoles_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id]) ON DELETE CASCADE
);

GO

CREATE INDEX [RoleNameIndex] ON [AspNetRoles] ([NormalizedName]);

GO

CREATE INDEX [IX_AspNetRoleClaims_RoleId] ON [AspNetRoleClaims] ([RoleId]);

GO

CREATE INDEX [IX_AspNetUserClaims_UserId] ON [AspNetUserClaims] ([UserId]);

GO

CREATE INDEX [IX_AspNetUserLogins_UserId] ON [AspNetUserLogins] ([UserId]);

GO

CREATE INDEX [IX_AspNetUserRoles_RoleId] ON [AspNetUserRoles] ([RoleId]);

GO

CREATE INDEX [IX_AspNetUserRoles_UserId] ON [AspNetUserRoles] ([UserId]);

GO

CREATE INDEX [EmailIndex] ON [AspNetUsers] ([NormalizedEmail]);

GO

CREATE UNIQUE INDEX [UserNameIndex] ON [AspNetUsers] ([NormalizedUserName]);

GO

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'00000000000000_CreateIdentitySchema', N'2.0.0-rtm-26452');

GO

DROP INDEX [UserNameIndex] ON [AspNetUsers];

GO

DROP INDEX [IX_AspNetUserRoles_UserId] ON [AspNetUserRoles];

GO

DROP INDEX [RoleNameIndex] ON [AspNetRoles];

GO

ALTER TABLE [AspNetUsers] ADD [NewColumn] nvarchar(max) NULL;

GO

CREATE UNIQUE INDEX [UserNameIndex] ON [AspNetUsers] ([NormalizedUserName]) WHERE [NormalizedUserName] IS NOT NULL;

GO

CREATE UNIQUE INDEX [RoleNameIndex] ON [AspNetRoles] ([NormalizedName]) WHERE [NormalizedName] IS NOT NULL;

GO

ALTER TABLE [AspNetUserTokens] ADD CONSTRAINT [FK_AspNetUserTokens_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id]) ON DELETE CASCADE;

GO

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20180105034732_AddNewColumn', N'2.0.0-rtm-26452');

GO

IF OBJECT_ID(N'__EFMigrationsHistory') IS NULL
BEGIN
CREATE TABLE [__EFMigrationsHistory] (
[MigrationId] nvarchar(150) NOT NULL,
[ProductVersion] nvarchar(32) NOT NULL,
CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
);
END; GO CREATE TABLE [AspNetRoles] (
[Id] nvarchar(450) NOT NULL,
[ConcurrencyStamp] nvarchar(max) NULL,
[Name] nvarchar(256) NULL,
[NormalizedName] nvarchar(256) NULL,
CONSTRAINT [PK_AspNetRoles] PRIMARY KEY ([Id])
); GO CREATE TABLE [AspNetUserTokens] (
[UserId] nvarchar(450) NOT NULL,
[LoginProvider] nvarchar(450) NOT NULL,
[Name] nvarchar(450) NOT NULL,
[Value] nvarchar(max) NULL,
CONSTRAINT [PK_AspNetUserTokens] PRIMARY KEY ([UserId], [LoginProvider], [Name])
); GO CREATE TABLE [AspNetUsers] (
[Id] nvarchar(450) NOT NULL,
[AccessFailedCount] int NOT NULL,
[ConcurrencyStamp] nvarchar(max) NULL,
[Email] nvarchar(256) NULL,
[EmailConfirmed] bit NOT NULL,
[LockoutEnabled] bit NOT NULL,
[LockoutEnd] datetimeoffset NULL,
[NormalizedEmail] nvarchar(256) NULL,
[NormalizedUserName] nvarchar(256) NULL,
[PasswordHash] nvarchar(max) NULL,
[PhoneNumber] nvarchar(max) NULL,
[PhoneNumberConfirmed] bit NOT NULL,
[SecurityStamp] nvarchar(max) NULL,
[TwoFactorEnabled] bit NOT NULL,
[UserName] nvarchar(256) NULL,
CONSTRAINT [PK_AspNetUsers] PRIMARY KEY ([Id])
); GO CREATE TABLE [AspNetRoleClaims] (
[Id] int NOT NULL IDENTITY,
[ClaimType] nvarchar(max) NULL,
[ClaimValue] nvarchar(max) NULL,
[RoleId] nvarchar(450) NOT NULL,
CONSTRAINT [PK_AspNetRoleClaims] PRIMARY KEY ([Id]),
CONSTRAINT [FK_AspNetRoleClaims_AspNetRoles_RoleId] FOREIGN KEY ([RoleId]) REFERENCES [AspNetRoles] ([Id]) ON DELETE CASCADE
); GO CREATE TABLE [AspNetUserClaims] (
[Id] int NOT NULL IDENTITY,
[ClaimType] nvarchar(max) NULL,
[ClaimValue] nvarchar(max) NULL,
[UserId] nvarchar(450) NOT NULL,
CONSTRAINT [PK_AspNetUserClaims] PRIMARY KEY ([Id]),
CONSTRAINT [FK_AspNetUserClaims_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id]) ON DELETE CASCADE
); GO CREATE TABLE [AspNetUserLogins] (
[LoginProvider] nvarchar(450) NOT NULL,
[ProviderKey] nvarchar(450) NOT NULL,
[ProviderDisplayName] nvarchar(max) NULL,
[UserId] nvarchar(450) NOT NULL,
CONSTRAINT [PK_AspNetUserLogins] PRIMARY KEY ([LoginProvider], [ProviderKey]),
CONSTRAINT [FK_AspNetUserLogins_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id]) ON DELETE CASCADE
); GO CREATE TABLE [AspNetUserRoles] (
[UserId] nvarchar(450) NOT NULL,
[RoleId] nvarchar(450) NOT NULL,
CONSTRAINT [PK_AspNetUserRoles] PRIMARY KEY ([UserId], [RoleId]),
CONSTRAINT [FK_AspNetUserRoles_AspNetRoles_RoleId] FOREIGN KEY ([RoleId]) REFERENCES [AspNetRoles] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_AspNetUserRoles_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id]) ON DELETE CASCADE
); GO CREATE INDEX [RoleNameIndex] ON [AspNetRoles] ([NormalizedName]); GO CREATE INDEX [IX_AspNetRoleClaims_RoleId] ON [AspNetRoleClaims] ([RoleId]); GO CREATE INDEX [IX_AspNetUserClaims_UserId] ON [AspNetUserClaims] ([UserId]); GO CREATE INDEX [IX_AspNetUserLogins_UserId] ON [AspNetUserLogins] ([UserId]); GO CREATE INDEX [IX_AspNetUserRoles_RoleId] ON [AspNetUserRoles] ([RoleId]); GO CREATE INDEX [IX_AspNetUserRoles_UserId] ON [AspNetUserRoles] ([UserId]); GO CREATE INDEX [EmailIndex] ON [AspNetUsers] ([NormalizedEmail]); GO CREATE UNIQUE INDEX [UserNameIndex] ON [AspNetUsers] ([NormalizedUserName]); GO INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'00000000000000_CreateIdentitySchema', N'2.0.0-rtm-26452'); GO DROP INDEX [UserNameIndex] ON [AspNetUsers]; GO DROP INDEX [IX_AspNetUserRoles_UserId] ON [AspNetUserRoles]; GO DROP INDEX [RoleNameIndex] ON [AspNetRoles]; GO ALTER TABLE [AspNetUsers] ADD [NewColumn] nvarchar(max) NULL; GO CREATE UNIQUE INDEX [UserNameIndex] ON [AspNetUsers] ([NormalizedUserName]) WHERE [NormalizedUserName] IS NOT NULL; GO CREATE UNIQUE INDEX [RoleNameIndex] ON [AspNetRoles] ([NormalizedName]) WHERE [NormalizedName] IS NOT NULL; GO ALTER TABLE [AspNetUserTokens] ADD CONSTRAINT [FK_AspNetUserTokens_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id]) ON DELETE CASCADE; GO INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20180105034732_AddNewColumn', N'2.0.0-rtm-26452'); GO

菜鸟入门【ASP.NET Core】13:Individual authentication 模板、EF Core Migration的更多相关文章

  1. 【ASP.NET Core快速入门】(十三)Individual authentication 模板、EF Core Migration

    Individual authentication 模板 我们首先用VSCode新建一个mvc的网站,这个网站创立的时候回自动为我们创建Identuty Core以及EF Core的代码示例,我们可以 ...

  2. 项目开发中的一些注意事项以及技巧总结 基于Repository模式设计项目架构—你可以参考的项目架构设计 Asp.Net Core中使用RSA加密 EF Core中的多对多映射如何实现? asp.net core下的如何给网站做安全设置 获取服务端https证书 Js异常捕获

    项目开发中的一些注意事项以及技巧总结   1.jquery采用ajax向后端请求时,MVC框架并不能返回View的数据,也就是一般我们使用View().PartialView()等,只能返回json以 ...

  3. 任务41:Individual authentication 模板

    使用命令行创建项目 会自动加上ef core和Identity core代码的示例 默认创建mvc是不带Identity的 dotnet new -help:查看可用的命令: -au:Individu ...

  4. asp.net core-15.Individual authentication 模板

    在visual studio code执行dotnet new mvc --help 可以查看执行命令 visual studio code先创建一个mvc的项目 dotnet new mvc -au ...

  5. 菜鸟入门【ASP.NET Core】5:命令行配置、Json文件配置、Bind读取配置到C#实例、在Core Mvc中使用Options

      命令行配置 我们通过vs2017创建一个控制台项目CommandLineSample 可以看到现在项目以来的是dotnet core framework 我们需要吧asp.net core引用进来 ...

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

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

  7. asp.net EF core 系列 作者:懒懒的程序员一枚

    asp.net core 系列 19 EFCore介绍写作逻辑一 .概述1.1 比较EF Core 和EF61.2 EF Core数据库提供程序 1.3 引用程序添加数据库提供程序1.4 获取Enti ...

  8. EF Core数据访问入门

    重要概念 Entity Framework (EF) Core 是轻量化.可扩展.开源和跨平台的数据访问技术,它还是一 种对象关系映射器 (ORM),它使 .NET 开发人员能够使用面向对象的思想处理 ...

  9. EF core 性能调优

    Entity Framework Core performance tuning – a worked example Last Updated: February 25, 2019 | Create ...

随机推荐

  1. CentOS7 启动中文输入法

    CentOS 系统中是带有中文输入法的(智能拼音),启动方式如下: Applications --> System Tools -->  Setting --> Region &am ...

  2. jedis set 的四个重载方法(byte[]的四个自动忽略)

    方法定义如下: 1.String set(String key, String value) 2.String set(String key, String value, String nxxx) 3 ...

  3. 2019.02.21 bzoj2300: [HAOI2011]防线修建(set+凸包)

    传送门 题意:动态维护凸包周长. 思路: 见这篇求面积的吧反正都是一个套路. 代码: #include<bits/stdc++.h> #define int long long #defi ...

  4. LOJ-10102(求A到B之间的割点)

    题目链接:传送门 思路:求A到B之间必要的中间节点 条件:(1)只有一条路径经过中间节点:(low[B]>=num[u]&&num[v]<=num[B],没有从B到u的路径 ...

  5. toast

     start (e){          wx.showToast({         title: '轮播图',         icon: 'success',         duration: ...

  6. 【python-HTMLTestRunner】生成HTMLTestRunner报告报错ERROR 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)

    [python-HTMLTestRunner]生成HTMLTestRunner报告报错:ERROR 'ascii' codec can't decode byte 0xe5 in position 0 ...

  7. 在Windows上安装Arduino的步骤

    在64位Windows 10机器上测试 Windows Installer从arduino.cc下载并安装最新的Arduino IDE 从git-scm.com下载并安装Git 开始Git GUI并运 ...

  8. python 使用 sorted 对 列表嵌套元组的数据进行排序

    在开发的过程可能会遇到这么一个需求,存在一个列表嵌套元组的数据: data = [(1, 'a'),(2, 'b'),(5, 'c'),(3, 'd'),(4, 'e')] 需要将这个列表按照元组的第 ...

  9. 机器学习基石笔记:06 Theory of Generalization

    若H的断点为k,即k个数据点不能被H给shatter,那么k+1个数据点也不能被H给shatter,即k+1也是H的断点. 如果给定的样本数N是大于等于k的,易得mH(N)<2N,且随着N的增大 ...

  10. LeetCode: 150_Evaluate Reverse Polish Notation | 分析逆波兰式 | Medium

    题目: Evaluate Reverse Polish Notation Evaluatethe value of an arithmetic expression in Reverse Polish ...