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

简介: 原文:ASP.NET Core Identity 迁移数据 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core Identity 迁移数据 上一章节中我们配置了 Identity ,也让我们的应用程序正常运行起来了,在访问未授权页面的时候会跳转到 /Accout/Login 页面。

原文: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 数据就迁移成功了

目录
相关文章
|
9天前
|
开发框架 监控 前端开发
在 ASP.NET Core Web API 中使用操作筛选器统一处理通用操作
【9月更文挑战第27天】操作筛选器是ASP.NET Core MVC和Web API中的一种过滤器,可在操作方法执行前后运行代码,适用于日志记录、性能监控和验证等场景。通过实现`IActionFilter`接口的`OnActionExecuting`和`OnActionExecuted`方法,可以统一处理日志、验证及异常。创建并注册自定义筛选器类,能提升代码的可维护性和复用性。
|
9天前
|
开发框架 .NET 中间件
ASP.NET Core Web 开发浅谈
本文介绍ASP.NET Core,一个轻量级、开源的跨平台框架,专为构建高性能Web应用设计。通过简单步骤,你将学会创建首个Web应用。文章还深入探讨了路由配置、依赖注入及安全性配置等常见问题,并提供了实用示例代码以助于理解与避免错误,帮助开发者更好地掌握ASP.NET Core的核心概念。
29 3
|
1月前
|
开发框架 前端开发 JavaScript
ASP.NET MVC 教程
ASP.NET 是一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架。
28 7
|
1月前
|
开发框架 NoSQL .NET
利用分布式锁在ASP.NET Core中实现防抖
【9月更文挑战第5天】在 ASP.NET Core 中,可通过分布式锁实现防抖功能,仅处理连续相同请求中的首个请求,其余请求返回 204 No Content,直至锁释放。具体步骤包括:安装分布式锁库如 `StackExchange.Redis`;创建分布式锁服务接口及其实现;构建防抖中间件;并在 `Startup.cs` 中注册相关服务和中间件。这一机制有效避免了短时间内重复操作的问题。
|
1月前
|
SQL 关系型数据库 数据库
七天.NET 8操作SQLite入门到实战详细教程(选型、开发、发布、部署)
七天.NET 8操作SQLite入门到实战详细教程(选型、开发、发布、部署)
|
2月前
|
开发框架 监控 .NET
开发者的革新利器:ASP.NET Core实战指南,构建未来Web应用的高效之道
【8月更文挑战第28天】本文探讨了如何利用ASP.NET Core构建高效、可扩展的Web应用。ASP.NET Core是一个开源、跨平台的框架,具有依赖注入、配置管理等特性。文章详细介绍了项目结构规划、依赖注入配置、中间件使用及性能优化方法,并讨论了安全性、可扩展性以及容器化的重要性。通过这些技术要点,开发者能够快速构建出符合现代Web应用需求的应用程序。
36 0
|
2月前
|
缓存 数据库连接 API
Entity Framework Core——.NET 领域的 ORM 利器,深度剖析其最佳实践之路
【8月更文挑战第28天】在软件开发领域,高效的数据访问与管理至关重要。Entity Framework Core(EF Core)作为一款强大的对象关系映射(ORM)工具,在 .NET 开发中扮演着重要角色。本文通过在线书店应用案例,展示了 EF Core 的核心特性和优势。我们定义了 `Book` 实体类及其属性,并通过 `BookStoreContext` 数据库上下文配置了数据库连接。EF Core 提供了简洁的 API,支持数据的查询、插入、更新和删除操作。
73 0
|
2月前
|
开发框架 监控 .NET
【Azure 应用程序见解】在Docker中运行的ASP.NET Core应用如何开启Application Insights的Profiler Trace呢?
【Azure 应用程序见解】在Docker中运行的ASP.NET Core应用如何开启Application Insights的Profiler Trace呢?
|
29天前
|
存储 开发框架 前端开发
ASP.NET MVC 迅速集成 SignalR
ASP.NET MVC 迅速集成 SignalR
39 0
|
2月前
|
开发框架 前端开发 .NET
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
37 0