'Move Identity Tables To Custom Schema Asp.Net Core
I Try To move all my identity Tables to Schema "User" using the following code in my context. it works fine for all except "AspNetUserClaims", "AspNetRoleClaims". What Should I do?
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");
}
Migration Relating to lines above :
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");
}
}
}
When ``` base.OnModelCreating(builder); is at the top , moves all except "AspNetUserClaims", "AspNetRoleClaims" . when its at the end it viseversa.
Solution 1:[1]
"AspNetUserClaims"
, "AspNetRoleClaims"
check id types you have now in db.
You have problems because thay not the same. In base it should be like:
builder.Entity<IdentityRoleClaim<string>>().ToTable("AspNetRoleClaims", schema: "User");
builder.Entity<IdentityUserClaim<string>>().ToTable("AspNetUserClaims", schema: "User");
If you not set them customly...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | cokeman19 |