原文:ASP.NET Core Identity 迁移数据 - ASP.NET Core 基础教程 - 简单教程,简单编程

ASP.NET Core Identity 迁移数据

上一章节中我们配置了 Identity ,也让我们的应用程序正常运行起来了,在访问未授权页面的时候会跳转到 /Accout/Login 页面。接下来我们就要实现登录的功能

但是,咦,好像还没创建用户表呢..哎呀,不是没创建用户表,而是没做 Identity 的数据迁移

那么,本章节接下来的内容,就开始迁移 Identity 数据呗

数据库连接配置

对了,我在看官方文档的过程中,发现了两种配置数据库连接的方法

一、第一种是在 Startup 类中的 ConfigureServices 方法中配置

services.AddEntityFrameworkSqlite().AddDbContext<HelloWorldDBContext>(options => options.UseSqlite(Configuration["database:connection"]));

这种方法需要我们的 HelloWorldDBContext 有一个可以接受 DbContextOptions<HelloWorldDBContext> 类型参数的构造函数

public HelloWorldDBContext(DbContextOptions<HelloWorldDBContext> options)
: base(options)
{
}

二、第二种方法只需要在 HelloWorldDBContext 类中重写方法 OnConfiguring

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
optionsBuilder.UseSqlite("Data Source=blogging.db");
}

然后在 Startup 类中的 ConfigureServices 方法种就可以这么写了

services.AddEntityFrameworkSqlite().AddDbContext<HelloWorldDBContext>();

看起来是不是第二种更简单更方便更简洁

后知后觉,醉了,这次,我们就使用这种方法吧,然后将其它相关的都统统删掉

  1. 首先删除 Startup 类中的 ConfigureServices 方法中的相关配置

    public void ConfigureServices(IServiceCollection services)
    {
    services.AddMvc(); services.AddEntityFrameworkSqlite().AddDbContext<HelloWorldDBContext>(); services.AddIdentity<User, IdentityRole>().AddEntityFrameworkStores<HelloWorldDBContext>(); }
  2. 其次在 HelloWorldDBContext 类中重写方法 OnConfiguring,且删除带参数的构造函数

    其实删除了带参数的构造函数,那么不带参数的构造函数也可以删除了,于是就变成了

    using System;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
    using Microsoft.Extensions.Configuration; namespace HelloWorld.Models
    {
    public class HelloWorldDBContext:IdentityDbContext<User>
    { public DbSet<Employee> Employees { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
    optionsBuilder.UseSqlite("Data Source=blogging.db");
    }
    }
    }

    这种方法我还没找到如何引用 Configuration 属性的方式,如果你知道,请麻烦告诉我下

重启下我们的应用程序,刷新下首页,看看是否正常输出

最佳实战

实际应用中,你千万不要像我这么激进,因为这两种方式是互补的关系,也就是说 OnConfiguring() 方法可以覆写传递给 HelloWorldDBContext(DbContextOptions<HelloWorldDBContext> options) 的连接

当一个应用需要连接多个数据库的时候,结合这两种方式非常有用,第一种方式用于默认的数据库,第二种方式用于特定的数据库

迁移 Identity 数据

好了,让我门言归正传吧,开始迁移 Identity 数据。其实前面的 EF 框架中我们已经迁移过一次了,不知道大家是否还记得如何迁移呢

  1. 首先停止我们的应用程序,不然会报错,因为我们使用的是文件系统数据库

  2. 其次打开 控制台窗口 ( 终端或 Power Shell 或 命令提示符 ),定位到我们的 HelloWorld 项目下

    $ cd ~/Developer/aspnetcore/HelloWorld/HelloWorld
    

    我用的是 macOS,且我的 HelloWorld 保存在 ~/Developer/aspnetcore/HelloWorld/HelloWorld

    然后使用 ls 命名就可以列出当前目录下的所有文件和子目录

    $ ls
    AppSettings.json Controllers HelloWorld.csproj Models Program.cs Properties Startup.cs Views bin obj wwwroot

    如果你使用的是 Windows,且使用的不是 Power Shell,那么你应该使用 dir 命令而不是 ls 命令

    如果能看到 HelloWorld.csproj 则说明处于正确的目录,否则你应该继续 cd 到该目录下

  3. 然后可以运行下列命令创建迁移代码

    dotnet ef migrations add InitialIdentity -v
    
    参数 说明
    dotnet 是 .NET 框架所有命令的开始标识
    ef 是指使用 Entity Framework 提供的命令
    migrations 是指使用迁移命令
    add 是指添加迁移
    InitialCreate 是本次迁移的说明,你可以改成任意文本,但我们推荐最好是能清楚的描述本次迁移的意图
    -v 参数用于输出创建迁移代码时的运行日志,方便出错时我们可以查看错在哪里

    运行结果如下

    $ dotnet ef migrations add InitialIdentity -v
    Using project '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/HelloWorld.csproj'.
    Using startup project '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/HelloWorld.csproj'.
    Writing '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/obj/HelloWorld.csproj.EntityFrameworkCore.targets'...
    dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=/var/folders/yk/2446sljj6hn82nvzkdgxltmw0000gn/T/tmpbjHVNk.tmp /verbosity:quiet /nologo /Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/HelloWorld.csproj
    Writing '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/obj/HelloWorld.csproj.EntityFrameworkCore.targets'...
    dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=/var/folders/yk/2446sljj6hn82nvzkdgxltmw0000gn/T/tmpDgtTNf.tmp /verbosity:quiet /nologo /Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/HelloWorld.csproj
    dotnet build /Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/HelloWorld.csproj /verbosity:quiet /nologo Build succeeded.
    0 Warning(s)
    0 Error(s) Time Elapsed 00:00:03.64
    dotnet exec --depsfile /Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/bin/Debug/netcoreapp2.1/HelloWorld.deps.json --additionalprobingpath /Users/yufei/.nuget/packages --additionalprobingpath /usr/local/share/dotnet/sdk/NuGetFallbackFolder --runtimeconfig /Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/bin/Debug/netcoreapp2.1/HelloWorld.runtimeconfig.json /usr/local/share/dotnet/sdk/2.1.301/DotnetTools/dotnet-ef/2.1.1/tools/netcoreapp2.1/any/tools/netcoreapp2.0/any/ef.dll migrations add InitialIdentity --assembly /Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/bin/Debug/netcoreapp2.1/HelloWorld.dll --startup-assembly /Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/bin/Debug/netcoreapp2.1/HelloWorld.dll --project-dir /Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/ --language C# --working-dir /Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld --verbose --root-namespace HelloWorld
    Using assembly 'HelloWorld'.
    Using startup assembly 'HelloWorld'.
    Using application base '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/bin/Debug/netcoreapp2.1'.
    Using working directory '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld'.
    Using root namespace 'HelloWorld'.
    Using project directory '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/'.
    Finding DbContext classes...
    Finding IDesignTimeDbContextFactory implementations...
    Finding application service provider...
    Finding IWebHost accessor...
    Using environment 'Development'.
    Using application service provider from IWebHost accessor on 'Program'.
    Found DbContext 'HelloWorldDBContext'.
    Finding DbContext classes in the project...
    Using context 'HelloWorldDBContext'.
    Finding design-time services for provider 'Microsoft.EntityFrameworkCore.Sqlite'...
    Using design-time services from provider 'Microsoft.EntityFrameworkCore.Sqlite'.
    Finding design-time services referenced by assembly 'HelloWorld'.
    No referenced design-time services were found.
    Finding IDesignTimeServices implementations in assembly 'HelloWorld'...
    No design-time services were found.
    DetectChanges starting for 'HelloWorldDBContext'.
    DetectChanges completed for 'HelloWorldDBContext'.
    DetectChanges starting for 'HelloWorldDBContext'.
    DetectChanges completed for 'HelloWorldDBContext'.
    DetectChanges starting for 'HelloWorldDBContext'.
    DetectChanges completed for 'HelloWorldDBContext'.
    DetectChanges starting for 'HelloWorldDBContext'.
    DetectChanges completed for 'HelloWorldDBContext'.
    Writing migration to '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/Migrations/20180624040354_InitialIdentity.cs'.
    Writing model snapshot to '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/Migrations/HelloWorldDBContextModelSnapshot.cs'.
    'HelloWorldDBContext' disposed.
    Done. To undo this action, use 'ef migrations remove'

    大概意思就是经过漫长的岁月,成功创建了迁移代码

    这一步非常容易出错,如果你有任何错误,欢迎你在讨论区留下你包括的意见

    ASP.NET Core 讨论区

  4. 创建迁移成功后,我们就可以使用下面的命令查看当前有多少迁移代码和它们的状态

    $ dotnet ef migrations list
    

    运行结果如下

    $ dotnet ef migrations list
    20180623011249_InitialCreate
    20180624040354_InitialIdentity

    可以看到总共有两个迁移代码,下面那个 20180624040354_InitialIdentity 就是我们刚刚创建

    在解决方案管理器中我们还可以看到 Migrations 又多了很多文件

  5. 接下来我们就要开始应用这些迁移来更新数据库,执行下面的命令来应用迁移代码

    $ dotnet ef database update -v
    
    参数 说明
    dotnet 是 .NET 框架所有命令的开始标识
    ef 是指使用 Entity Framework 提供的命令
    database 是指使用数据库相关命令
    update 是指更新数据库
    -v 参数用于输出创建迁移代码时的运行日志,方便出错时我们可以查看错在哪里

    执行以上命令,输出结果如下

    $ dotnet ef database update -v
    Using project '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/HelloWorld.csproj'.
    Using startup project '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/HelloWorld.csproj'.
    Writing '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/obj/HelloWorld.csproj.EntityFrameworkCore.targets'...
    dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=/var/folders/yk/2446sljj6hn82nvzkdgxltmw0000gn/T/tmp6jVQsB.tmp /verbosity:quiet /nologo /Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/HelloWorld.csproj
    Writing '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/obj/HelloWorld.csproj.EntityFrameworkCore.targets'...
    dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=/var/folders/yk/2446sljj6hn82nvzkdgxltmw0000gn/T/tmpXNRfCA.tmp /verbosity:quiet /nologo /Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/HelloWorld.csproj
    dotnet build /Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/HelloWorld.csproj /verbosity:quiet /nologo Build succeeded.
    0 Warning(s)
    0 Error(s) Time Elapsed 00:00:04.59
    dotnet exec --depsfile /Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/bin/Debug/netcoreapp2.1/HelloWorld.deps.json --additionalprobingpath /Users/yufei/.nuget/packages --additionalprobingpath /usr/local/share/dotnet/sdk/NuGetFallbackFolder --runtimeconfig /Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/bin/Debug/netcoreapp2.1/HelloWorld.runtimeconfig.json /usr/local/share/dotnet/sdk/2.1.301/DotnetTools/dotnet-ef/2.1.1/tools/netcoreapp2.1/any/tools/netcoreapp2.0/any/ef.dll database update --assembly /Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/bin/Debug/netcoreapp2.1/HelloWorld.dll --startup-assembly /Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/bin/Debug/netcoreapp2.1/HelloWorld.dll --project-dir /Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/ --language C# --working-dir /Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld --verbose --root-namespace HelloWorld
    Using assembly 'HelloWorld'.
    Using startup assembly 'HelloWorld'.
    Using application base '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/bin/Debug/netcoreapp2.1'.
    Using working directory '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld'.
    Using root namespace 'HelloWorld'.
    Using project directory '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/'.
    Finding DbContext classes...
    Finding IDesignTimeDbContextFactory implementations...
    Finding application service provider...
    Finding IWebHost accessor...
    Using environment 'Development'.
    Using application service provider from IWebHost accessor on 'Program'.
    Found DbContext 'HelloWorldDBContext'.
    Finding DbContext classes in the project...
    Using context 'HelloWorldDBContext'.
    Finding design-time services for provider 'Microsoft.EntityFrameworkCore.Sqlite'...
    Using design-time services from provider 'Microsoft.EntityFrameworkCore.Sqlite'.
    Finding design-time services referenced by assembly 'HelloWorld'.
    No referenced design-time services were found.
    Finding IDesignTimeServices implementations in assembly 'HelloWorld'...
    No design-time services were found.
    Migrating using database 'main' on server 'blogging.db'.
    Opening connection to database 'main' on server 'blogging.db'.
    Opened connection to database 'main' on server '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/blogging.db'.
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    PRAGMA foreign_keys=ON;
    Executed DbCommand (33ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    PRAGMA foreign_keys=ON;
    Opening connection to database 'main' on server 'blogging.db'.
    Opened connection to database 'main' on server '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/blogging.db'.
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    PRAGMA foreign_keys=ON;
    Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    PRAGMA foreign_keys=ON;
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table';
    Executed DbCommand (9ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table';
    Closing connection to database 'main' on server '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/blogging.db'.
    Closed connection to database 'main' on server 'blogging.db'.
    Opening connection to database 'main' on server 'blogging.db'.
    Opened connection to database 'main' on server '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/blogging.db'.
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    PRAGMA foreign_keys=ON;
    Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    PRAGMA foreign_keys=ON;
    Opening connection to database 'main' on server 'blogging.db'.
    Opened connection to database 'main' on server '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/blogging.db'.
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    PRAGMA foreign_keys=ON;
    Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    PRAGMA foreign_keys=ON;
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table';
    Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table';
    Closing connection to database 'main' on server '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/blogging.db'.
    Closed connection to database 'main' on server 'blogging.db'.
    Opening connection to database 'main' on server 'blogging.db'.
    Opened connection to database 'main' on server '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/blogging.db'.
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    PRAGMA foreign_keys=ON;
    Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    PRAGMA foreign_keys=ON;
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    SELECT "MigrationId", "ProductVersion"
    FROM "__EFMigrationsHistory"
    ORDER BY "MigrationId";
    Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    SELECT "MigrationId", "ProductVersion"
    FROM "__EFMigrationsHistory"
    ORDER BY "MigrationId";
    Closing connection to database 'main' on server '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/blogging.db'.
    Closed connection to database 'main' on server 'blogging.db'.
    A data reader was disposed.
    Applying migration '20180624040354_InitialIdentity'.
    Opening connection to database 'main' on server 'blogging.db'.
    Opened connection to database 'main' on server '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/blogging.db'.
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    PRAGMA foreign_keys=ON;
    Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    PRAGMA foreign_keys=ON;
    Beginning transaction with isolation level 'Serializable'.
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE TABLE "AspNetRoles" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_AspNetRoles" PRIMARY KEY,
    "Name" TEXT NULL,
    "NormalizedName" TEXT NULL,
    "ConcurrencyStamp" TEXT NULL
    );
    Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE TABLE "AspNetRoles" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_AspNetRoles" PRIMARY KEY,
    "Name" TEXT NULL,
    "NormalizedName" TEXT NULL,
    "ConcurrencyStamp" TEXT NULL
    );
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE TABLE "AspNetUsers" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_AspNetUsers" PRIMARY KEY,
    "UserName" TEXT NULL,
    "NormalizedUserName" TEXT NULL,
    "Email" TEXT NULL,
    "NormalizedEmail" TEXT NULL,
    "EmailConfirmed" INTEGER NOT NULL,
    "PasswordHash" TEXT NULL,
    "SecurityStamp" TEXT NULL,
    "ConcurrencyStamp" TEXT NULL,
    "PhoneNumber" TEXT NULL,
    "PhoneNumberConfirmed" INTEGER NOT NULL,
    "TwoFactorEnabled" INTEGER NOT NULL,
    "LockoutEnd" TEXT NULL,
    "LockoutEnabled" INTEGER NOT NULL,
    "AccessFailedCount" INTEGER NOT NULL
    );
    Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE TABLE "AspNetUsers" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_AspNetUsers" PRIMARY KEY,
    "UserName" TEXT NULL,
    "NormalizedUserName" TEXT NULL,
    "Email" TEXT NULL,
    "NormalizedEmail" TEXT NULL,
    "EmailConfirmed" INTEGER NOT NULL,
    "PasswordHash" TEXT NULL,
    "SecurityStamp" TEXT NULL,
    "ConcurrencyStamp" TEXT NULL,
    "PhoneNumber" TEXT NULL,
    "PhoneNumberConfirmed" INTEGER NOT NULL,
    "TwoFactorEnabled" INTEGER NOT NULL,
    "LockoutEnd" TEXT NULL,
    "LockoutEnabled" INTEGER NOT NULL,
    "AccessFailedCount" INTEGER NOT NULL
    );
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE TABLE "AspNetRoleClaims" (
    "Id" INTEGER NOT NULL CONSTRAINT "PK_AspNetRoleClaims" PRIMARY KEY AUTOINCREMENT,
    "RoleId" TEXT NOT NULL,
    "ClaimType" TEXT NULL,
    "ClaimValue" TEXT NULL,
    CONSTRAINT "FK_AspNetRoleClaims_AspNetRoles_RoleId" FOREIGN KEY ("RoleId") REFERENCES "AspNetRoles" ("Id") ON DELETE CASCADE
    );
    Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE TABLE "AspNetRoleClaims" (
    "Id" INTEGER NOT NULL CONSTRAINT "PK_AspNetRoleClaims" PRIMARY KEY AUTOINCREMENT,
    "RoleId" TEXT NOT NULL,
    "ClaimType" TEXT NULL,
    "ClaimValue" TEXT NULL,
    CONSTRAINT "FK_AspNetRoleClaims_AspNetRoles_RoleId" FOREIGN KEY ("RoleId") REFERENCES "AspNetRoles" ("Id") ON DELETE CASCADE
    );
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE TABLE "AspNetUserClaims" (
    "Id" INTEGER NOT NULL CONSTRAINT "PK_AspNetUserClaims" PRIMARY KEY AUTOINCREMENT,
    "UserId" TEXT NOT NULL,
    "ClaimType" TEXT NULL,
    "ClaimValue" TEXT NULL,
    CONSTRAINT "FK_AspNetUserClaims_AspNetUsers_UserId" FOREIGN KEY ("UserId") REFERENCES "AspNetUsers" ("Id") ON DELETE CASCADE
    );
    Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE TABLE "AspNetUserClaims" (
    "Id" INTEGER NOT NULL CONSTRAINT "PK_AspNetUserClaims" PRIMARY KEY AUTOINCREMENT,
    "UserId" TEXT NOT NULL,
    "ClaimType" TEXT NULL,
    "ClaimValue" TEXT NULL,
    CONSTRAINT "FK_AspNetUserClaims_AspNetUsers_UserId" FOREIGN KEY ("UserId") REFERENCES "AspNetUsers" ("Id") ON DELETE CASCADE
    );
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE TABLE "AspNetUserLogins" (
    "LoginProvider" TEXT NOT NULL,
    "ProviderKey" TEXT NOT NULL,
    "ProviderDisplayName" TEXT NULL,
    "UserId" TEXT NOT NULL,
    CONSTRAINT "PK_AspNetUserLogins" PRIMARY KEY ("LoginProvider", "ProviderKey"),
    CONSTRAINT "FK_AspNetUserLogins_AspNetUsers_UserId" FOREIGN KEY ("UserId") REFERENCES "AspNetUsers" ("Id") ON DELETE CASCADE
    );
    Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE TABLE "AspNetUserLogins" (
    "LoginProvider" TEXT NOT NULL,
    "ProviderKey" TEXT NOT NULL,
    "ProviderDisplayName" TEXT NULL,
    "UserId" TEXT NOT NULL,
    CONSTRAINT "PK_AspNetUserLogins" PRIMARY KEY ("LoginProvider", "ProviderKey"),
    CONSTRAINT "FK_AspNetUserLogins_AspNetUsers_UserId" FOREIGN KEY ("UserId") REFERENCES "AspNetUsers" ("Id") ON DELETE CASCADE
    );
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE TABLE "AspNetUserRoles" (
    "UserId" TEXT NOT NULL,
    "RoleId" TEXT 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
    );
    Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE TABLE "AspNetUserRoles" (
    "UserId" TEXT NOT NULL,
    "RoleId" TEXT 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
    );
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE TABLE "AspNetUserTokens" (
    "UserId" TEXT NOT NULL,
    "LoginProvider" TEXT NOT NULL,
    "Name" TEXT NOT NULL,
    "Value" TEXT NULL,
    CONSTRAINT "PK_AspNetUserTokens" PRIMARY KEY ("UserId", "LoginProvider", "Name"),
    CONSTRAINT "FK_AspNetUserTokens_AspNetUsers_UserId" FOREIGN KEY ("UserId") REFERENCES "AspNetUsers" ("Id") ON DELETE CASCADE
    );
    Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE TABLE "AspNetUserTokens" (
    "UserId" TEXT NOT NULL,
    "LoginProvider" TEXT NOT NULL,
    "Name" TEXT NOT NULL,
    "Value" TEXT NULL,
    CONSTRAINT "PK_AspNetUserTokens" PRIMARY KEY ("UserId", "LoginProvider", "Name"),
    CONSTRAINT "FK_AspNetUserTokens_AspNetUsers_UserId" FOREIGN KEY ("UserId") REFERENCES "AspNetUsers" ("Id") ON DELETE CASCADE
    );
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE INDEX "IX_AspNetRoleClaims_RoleId" ON "AspNetRoleClaims" ("RoleId");
    Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE INDEX "IX_AspNetRoleClaims_RoleId" ON "AspNetRoleClaims" ("RoleId");
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE UNIQUE INDEX "RoleNameIndex" ON "AspNetRoles" ("NormalizedName");
    Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE UNIQUE INDEX "RoleNameIndex" ON "AspNetRoles" ("NormalizedName");
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE INDEX "IX_AspNetUserClaims_UserId" ON "AspNetUserClaims" ("UserId");
    Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE INDEX "IX_AspNetUserClaims_UserId" ON "AspNetUserClaims" ("UserId");
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE INDEX "IX_AspNetUserLogins_UserId" ON "AspNetUserLogins" ("UserId");
    Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE INDEX "IX_AspNetUserLogins_UserId" ON "AspNetUserLogins" ("UserId");
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE INDEX "IX_AspNetUserRoles_RoleId" ON "AspNetUserRoles" ("RoleId");
    Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE INDEX "IX_AspNetUserRoles_RoleId" ON "AspNetUserRoles" ("RoleId");
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE INDEX "EmailIndex" ON "AspNetUsers" ("NormalizedEmail");
    Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE INDEX "EmailIndex" ON "AspNetUsers" ("NormalizedEmail");
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE UNIQUE INDEX "UserNameIndex" ON "AspNetUsers" ("NormalizedUserName");
    Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE UNIQUE INDEX "UserNameIndex" ON "AspNetUsers" ("NormalizedUserName");
    Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
    INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
    VALUES ('20180624040354_InitialIdentity', '2.1.1-rtm-30846');
    Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
    VALUES ('20180624040354_InitialIdentity', '2.1.1-rtm-30846');
    Committing transaction.
    Disposing transaction.
    Closing connection to database 'main' on server '/Users/yufei/Developer/aspnetcore/HelloWorld/HelloWorld/blogging.db'.
    Closed connection to database 'main' on server 'blogging.db'.
    'HelloWorldDBContext' disposed.
    Done.

    输出日志很长,大概的意思就是执行成功了

使用 SQLite Studio 可以看到我们的 blogging.db 中已经有了表 Employees ,且有两个字段

可以看到多了很多的表

到此,Identity 数据就迁移成功了

ASP.NET Core Identity 迁移数据 - ASP.NET Core 基础教程 - 简单教程,简单编程的更多相关文章

  1. ASP.NET Core Identity 验证特性 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core Identity 验证特性 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core Identity 验证特性 上一章节我们简单介绍了 ...

  2. ASP.NET MVC应用迁移到ASP.NET Core及其异同简介

    ASP.NET Core是微软新推出支持跨平台.高性能.开源的开发框架,相比起原有的ASP.NET来说,ASP.NET Core更适合开发现代应用程序,如跨平台.Dorker的支持.集成现代前端开发框 ...

  3. DDD Code First 迁移数据实现EF CORE的软删除,值对象迁移配置

    感谢Jeffcky大佬的博客: EntityFramework Core 2.0全局过滤 (HasQueryFilter) https://www.cnblogs.com/CreateMyself/p ...

  4. IdentityServer(12)- 使用 ASP.NET Core Identity

    IdentityServer具有非常好的扩展性,其中用户及其数据(包括密码)部分你可以使用任何想要的数据库进行持久化. 如果需要一个新的用户数据库,那么ASP.NET Core Identity是你的 ...

  5. ASP.NET Core Identity Hands On(1)——Identity 初次体验

    ASP.NET Core Identity是用于构建ASP.NET Core Web应用程序的成员资格系统,包括成员资格.登录和用户数据存储 这是来自于 ASP.NET Core Identity 仓 ...

  6. IdentityServer4 中文文档 -14- (快速入门)使用 ASP.NET Core Identity

    IdentityServer4 中文文档 -14- (快速入门)使用 ASP.NET Core Identity 原文:http://docs.identityserver.io/en/release ...

  7. ASP.NET Core Identity自定义数据库结构和完全使用Dapper而非EntityFramework Core

    前言 原本本节内容是不存在的,出于有几个人问到了我:我想使用ASP.NET Core Identity,但是我又不想使用默认生成的数据库表,想自定义一套,我想要使用ASP.NE Core Identi ...

  8. 使用WebApi和Asp.Net Core Identity 认证 Blazor WebAssembly(Blazor客户端应用)

    原文:https://chrissainty.com/securing-your-blazor-apps-authentication-with-clientside-blazor-using-web ...

  9. Asp.Net Core Identity 完成注册登录

    Identity是Asp.Net Core全新的一个用户管理系统,它是一个完善的全面的庞大的框架,提供的功能有: 创建.查询.更改.删除账户信息 验证和授权 密码重置 双重身份认证 支持扩展登录,如微 ...

随机推荐

  1. css3-10 如何使用滚动条

    css3-10 如何使用滚动条 一.总结 一句话总结:给设置了宽高的块标签使用,直接将overflow属性写到style里面即可. 1.滚动条的使用对象时谁? 一般是div,当div比较小(设置了宽高 ...

  2. gerrit docker

    修改项目配置文件 git clone ssh://admin@localhost:29418/All-Projects && scp -p -P 29418 admin@localho ...

  3. 多校 hdu

    欢迎參加--每周六晚的BestCoder(有米!) Solve this interesting problem Time Limit: 2000/1000 MS (Java/Others)    M ...

  4. Javascript 获取页面高度(多种浏览器)

    //2015年8月13日11:00:50 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: d ...

  5. Home界面的启动

    继上篇文章Launcher进程的启动,我们继续分析Home界面的启动. public final class ActivityThread { ...... public static final v ...

  6. MySQL搜索:WHERE

    MySQL指定搜索条件进行搜索能够使用where条件. 在SELECT语句中.数据依据WHERE子语句中指定的条件进行过滤,WHERE子语句在表名之后给出. product表例如以下: a 查找价格等 ...

  7. Android 添加常驻图标到状态栏

    / * * 如果没有从状态栏中删除ICON,且继续调用addIconToStatusbar,则不会有任何变化.如果将notification中的resId设置不同的图标,则会显示不同的图标 * / p ...

  8. UUIDUtils工具类

    原理是根据自身的操作系统和电脑硬件生成的一个32位的随机字符串,如果是一台电脑使用的话,一年不会重复,经过java之后貌似编程了36位,多了4个"-",下面是代码实现: impor ...

  9. ArcSDE中Compress与Compact的区别

    原文 ArcSDE中Compress与Compact的区别 附件一”为两种数据库需要的管理工作.      与所表示的含义与操作是不同的.     对于来说,Compressing与Smart Dat ...

  10. POJ1659Frogs&#39; Neighborhood(lavel定理)

    Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 7260   Accepted: 31 ...