首页 > 解决方案 > 将标识表移动到自定义架构 Asp.Net Core

问题描述

我尝试在我的上下文中使用以下代码将我的所有身份表移动到模式“用户”。它适用于除“AspNetUserClaims”、“AspNetRoleClaims”之外的所有用户。我应该怎么办?

       protected override void OnModelCreating(ModelBuilder builder)
       {
           base.OnModelCreating(builder);

           builder.Entity<IdentityRole>().ToTable("AspNetRoles", schema: "User");
           builder.Entity<IdentityRoleClaim<int>>().ToTable("AspNetRoleClaims", schema: "User");
           builder.Entity<ApplicationUser>().ToTable("AspNetUsers", schema: "User");
           builder.Entity<IdentityUserClaim<int>>().ToTable("AspNetUserClaims", schema: "User");
           builder.Entity<IdentityUserLogin<string>>().ToTable("AspNetUserLogins", schema: "User");
           builder.Entity<IdentityUserRole<string>>().ToTable("AspNetUserRoles", schema: "User");
           builder.Entity<IdentityUserToken<string>>().ToTable("AspNetUserTokens", schema: "User");

       }

与上述行相关的迁移:

using Microsoft.EntityFrameworkCore.Migrations;

namespace MG.Data.Migrations
{
    public partial class Mig2 : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.RenameTable(
                name: "AspNetUserTokens",
                newName: "AspNetUserTokens",
                newSchema: "User");

            migrationBuilder.RenameTable(
                name: "AspNetUsers",
                newName: "AspNetUsers",
                newSchema: "User");

            migrationBuilder.RenameTable(
                name: "AspNetUserRoles",
                newName: "AspNetUserRoles",
                newSchema: "User");

            migrationBuilder.RenameTable(
                name: "AspNetUserLogins",
                newName: "AspNetUserLogins",
                newSchema: "User");

            migrationBuilder.RenameTable(
                name: "AspNetRoles",
                newName: "AspNetRoles",
                newSchema: "User");

            migrationBuilder.CreateTable(
                name: "AspNetRoleClaims",
                schema: "User",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    RoleId = table.Column<int>(type: "int", nullable: false),
                    ClaimType = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    ClaimValue = table.Column<string>(type: "nvarchar(max)", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AspNetRoleClaims", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "AspNetUserClaims",
                schema: "User",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    UserId = table.Column<int>(type: "int", nullable: false),
                    ClaimType = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    ClaimValue = table.Column<string>(type: "nvarchar(max)", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AspNetUserClaims", x => x.Id);
                });
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "AspNetRoleClaims",
                schema: "User");

            migrationBuilder.DropTable(
                name: "AspNetUserClaims",
                schema: "User");

            migrationBuilder.RenameTable(
                name: "AspNetUserTokens",
                schema: "User",
                newName: "AspNetUserTokens");

            migrationBuilder.RenameTable(
                name: "AspNetUsers",
                schema: "User",
                newName: "AspNetUsers");

            migrationBuilder.RenameTable(
                name: "AspNetUserRoles",
                schema: "User",
                newName: "AspNetUserRoles");

            migrationBuilder.RenameTable(
                name: "AspNetUserLogins",
                schema: "User",
                newName: "AspNetUserLogins");

            migrationBuilder.RenameTable(
                name: "AspNetRoles",
                schema: "User",
                newName: "AspNetRoles");
        }
    }
}

当``base.OnModelCreating(builder); 位于顶部,移动除 "AspNetUserClaims"、"AspNetRoleClaims" 之外的所有内容。当它在最后它viseversa。

标签: entity-frameworkasp.net-coreasp.net-identityef-core-5.0

解决方案


推荐阅读