身份 2.0 代码优先 Table 重命名
Identity 2.0 Code First Table Renaming
我正在尝试将身份 table 重命名为 Roles、Users、UserRoles、UserLogins 和 UserClaims。我部分成功地执行并使用命令 Update-Table 更新了我的数据库。
但是我似乎无法摆脱 AspNetUsers table,它总是只生成一列,即 Id 列,尽管我得到了另一个用户 table列列表和另一个 Id 列。
Update-Database生成的脚本
Applying automatic migration: 201501190035078_AutomaticMigration.
CREATE TABLE [dbo].[Roles] (
[Id] [nvarchar](128) NOT NULL,
[Name] [nvarchar](256) NOT NULL,
CONSTRAINT [PK_dbo.Roles] PRIMARY KEY ([Id])
)
CREATE TABLE [dbo].[UserRoles] (
[UserId] [nvarchar](128) NOT NULL,
[RoleId] [nvarchar](128) NOT NULL,
[IdentityUser_Id] [nvarchar](128),
CONSTRAINT [PK_dbo.UserRoles] PRIMARY KEY ([UserId], [RoleId])
)
CREATE TABLE [dbo].[Users] (
[Id] [nvarchar](128) NOT NULL,
[Email] [nvarchar](max),
[EmailConfirmed] [bit] NOT NULL,
[PasswordHash] [nvarchar](max),
[SecurityStamp] [nvarchar](max),
[PhoneNumber] [nvarchar](max),
[PhoneNumberConfirmed] [bit] NOT NULL,
[TwoFactorEnabled] [bit] NOT NULL,
[LockoutEndDateUtc] [datetime],
[LockoutEnabled] [bit] NOT NULL,
[AccessFailedCount] [int] NOT NULL,
[UserName] [nvarchar](max),
CONSTRAINT [PK_dbo.Users] PRIMARY KEY ([Id])
)
CREATE TABLE [dbo].[UserClaims] (
[Id] [int] NOT NULL IDENTITY,
[UserId] [nvarchar](max),
[ClaimType] [nvarchar](max),
[ClaimValue] [nvarchar](max),
[IdentityUser_Id] [nvarchar](128),
CONSTRAINT [PK_dbo.UserClaims] PRIMARY KEY ([Id])
)
CREATE TABLE [dbo].[UserLogins] (
[LoginProvider] [nvarchar](128) NOT NULL,
[ProviderKey] [nvarchar](128) NOT NULL,
[UserId] [nvarchar](128) NOT NULL,
[IdentityUser_Id] [nvarchar](128),
CONSTRAINT [PK_dbo.UserLogins] PRIMARY KEY ([LoginProvider], [ProviderKey], [UserId])
)
CREATE TABLE [dbo].[AspNetUsers] (
[Id] [nvarchar](128) NOT NULL,
CONSTRAINT [PK_dbo.AspNetUsers] PRIMARY KEY ([Id])
)
CREATE UNIQUE INDEX [RoleNameIndex] ON [dbo].[Roles]([Name])
CREATE INDEX [IX_RoleId] ON [dbo].[UserRoles]([RoleId])
CREATE INDEX [IX_IdentityUser_Id] ON [dbo].[UserRoles]([IdentityUser_Id])
CREATE INDEX [IX_IdentityUser_Id] ON [dbo].[UserClaims]([IdentityUser_Id])
CREATE INDEX [IX_IdentityUser_Id] ON [dbo].[UserLogins]([IdentityUser_Id])
CREATE INDEX [IX_Id] ON [dbo].[AspNetUsers]([Id])
ALTER TABLE [dbo].[UserRoles] ADD CONSTRAINT [FK_dbo.UserRoles_dbo.Roles_RoleId] FOREIGN KEY ([RoleId]) REFERENCES [dbo].[Roles] ([Id]) ON DELETE CASCADE
ALTER TABLE [dbo].[UserRoles] ADD CONSTRAINT [FK_dbo.UserRoles_dbo.Users_IdentityUser_Id] FOREIGN KEY ([IdentityUser_Id]) REFERENCES [dbo].[Users] ([Id])
ALTER TABLE [dbo].[UserClaims] ADD CONSTRAINT [FK_dbo.UserClaims_dbo.Users_IdentityUser_Id] FOREIGN KEY ([IdentityUser_Id]) REFERENCES [dbo].[Users] ([Id])
ALTER TABLE [dbo].[UserLogins] ADD CONSTRAINT [FK_dbo.UserLogins_dbo.Users_IdentityUser_Id] FOREIGN KEY ([IdentityUser_Id]) REFERENCES [dbo].[Users] ([Id])
ALTER TABLE [dbo].[AspNetUsers] ADD CONSTRAINT [FK_dbo.AspNetUsers_dbo.Users_Id] FOREIGN KEY ([Id]) REFERENCES [dbo].[Users] ([Id])
CREATE TABLE [dbo].[__MigrationHistory] (
[MigrationId] [nvarchar](150) NOT NULL,
[ContextKey] [nvarchar](300) NOT NULL,
[Model] [varbinary](max) NOT NULL,
[ProductVersion] [nvarchar](32) NOT NULL,
CONSTRAINT [PK_dbo.__MigrationHistory] PRIMARY KEY ([MigrationId], [ContextK
ey])
)
我的数据库上下文:
namespace Carbon.Models {
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using Microsoft.AspNet.Identity.EntityFramework;
public partial class CarbonEDM : IdentityDbContext<ApplicationUser> {
public CarbonEDM()
: base("name=CarbonDB") {
}
public static CarbonEDM Create() {
return new CarbonEDM();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>().ToTable("Users", "dbo");
modelBuilder.Entity<IdentityRole>().ToTable("Roles", "dbo");
modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles", "dbo");
modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims", "dbo");
modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins", "dbo");
}
}
}
您必须继承自 IdentityUser class。 (您是否有继承自 IdentityUser 的自定义用户 class?)。如果是这种情况,请不要覆盖 IdentityUser 的 table 名称,而只覆盖您的自定义用户 class。假设您的自定义用户 class 是 ApplicationUser:
modelBuilder.Entity<ApplicationUser>().ToTable("Users");
删除或评论以下行
//modelBuilder.Entity<IdentityUser>().ToTable("Users", "dbo");
我正在尝试将身份 table 重命名为 Roles、Users、UserRoles、UserLogins 和 UserClaims。我部分成功地执行并使用命令 Update-Table 更新了我的数据库。
但是我似乎无法摆脱 AspNetUsers table,它总是只生成一列,即 Id 列,尽管我得到了另一个用户 table列列表和另一个 Id 列。
Update-Database生成的脚本
Applying automatic migration: 201501190035078_AutomaticMigration.
CREATE TABLE [dbo].[Roles] (
[Id] [nvarchar](128) NOT NULL,
[Name] [nvarchar](256) NOT NULL,
CONSTRAINT [PK_dbo.Roles] PRIMARY KEY ([Id])
)
CREATE TABLE [dbo].[UserRoles] (
[UserId] [nvarchar](128) NOT NULL,
[RoleId] [nvarchar](128) NOT NULL,
[IdentityUser_Id] [nvarchar](128),
CONSTRAINT [PK_dbo.UserRoles] PRIMARY KEY ([UserId], [RoleId])
)
CREATE TABLE [dbo].[Users] (
[Id] [nvarchar](128) NOT NULL,
[Email] [nvarchar](max),
[EmailConfirmed] [bit] NOT NULL,
[PasswordHash] [nvarchar](max),
[SecurityStamp] [nvarchar](max),
[PhoneNumber] [nvarchar](max),
[PhoneNumberConfirmed] [bit] NOT NULL,
[TwoFactorEnabled] [bit] NOT NULL,
[LockoutEndDateUtc] [datetime],
[LockoutEnabled] [bit] NOT NULL,
[AccessFailedCount] [int] NOT NULL,
[UserName] [nvarchar](max),
CONSTRAINT [PK_dbo.Users] PRIMARY KEY ([Id])
)
CREATE TABLE [dbo].[UserClaims] (
[Id] [int] NOT NULL IDENTITY,
[UserId] [nvarchar](max),
[ClaimType] [nvarchar](max),
[ClaimValue] [nvarchar](max),
[IdentityUser_Id] [nvarchar](128),
CONSTRAINT [PK_dbo.UserClaims] PRIMARY KEY ([Id])
)
CREATE TABLE [dbo].[UserLogins] (
[LoginProvider] [nvarchar](128) NOT NULL,
[ProviderKey] [nvarchar](128) NOT NULL,
[UserId] [nvarchar](128) NOT NULL,
[IdentityUser_Id] [nvarchar](128),
CONSTRAINT [PK_dbo.UserLogins] PRIMARY KEY ([LoginProvider], [ProviderKey], [UserId])
)
CREATE TABLE [dbo].[AspNetUsers] (
[Id] [nvarchar](128) NOT NULL,
CONSTRAINT [PK_dbo.AspNetUsers] PRIMARY KEY ([Id])
)
CREATE UNIQUE INDEX [RoleNameIndex] ON [dbo].[Roles]([Name])
CREATE INDEX [IX_RoleId] ON [dbo].[UserRoles]([RoleId])
CREATE INDEX [IX_IdentityUser_Id] ON [dbo].[UserRoles]([IdentityUser_Id])
CREATE INDEX [IX_IdentityUser_Id] ON [dbo].[UserClaims]([IdentityUser_Id])
CREATE INDEX [IX_IdentityUser_Id] ON [dbo].[UserLogins]([IdentityUser_Id])
CREATE INDEX [IX_Id] ON [dbo].[AspNetUsers]([Id])
ALTER TABLE [dbo].[UserRoles] ADD CONSTRAINT [FK_dbo.UserRoles_dbo.Roles_RoleId] FOREIGN KEY ([RoleId]) REFERENCES [dbo].[Roles] ([Id]) ON DELETE CASCADE
ALTER TABLE [dbo].[UserRoles] ADD CONSTRAINT [FK_dbo.UserRoles_dbo.Users_IdentityUser_Id] FOREIGN KEY ([IdentityUser_Id]) REFERENCES [dbo].[Users] ([Id])
ALTER TABLE [dbo].[UserClaims] ADD CONSTRAINT [FK_dbo.UserClaims_dbo.Users_IdentityUser_Id] FOREIGN KEY ([IdentityUser_Id]) REFERENCES [dbo].[Users] ([Id])
ALTER TABLE [dbo].[UserLogins] ADD CONSTRAINT [FK_dbo.UserLogins_dbo.Users_IdentityUser_Id] FOREIGN KEY ([IdentityUser_Id]) REFERENCES [dbo].[Users] ([Id])
ALTER TABLE [dbo].[AspNetUsers] ADD CONSTRAINT [FK_dbo.AspNetUsers_dbo.Users_Id] FOREIGN KEY ([Id]) REFERENCES [dbo].[Users] ([Id])
CREATE TABLE [dbo].[__MigrationHistory] (
[MigrationId] [nvarchar](150) NOT NULL,
[ContextKey] [nvarchar](300) NOT NULL,
[Model] [varbinary](max) NOT NULL,
[ProductVersion] [nvarchar](32) NOT NULL,
CONSTRAINT [PK_dbo.__MigrationHistory] PRIMARY KEY ([MigrationId], [ContextK
ey])
)
我的数据库上下文:
namespace Carbon.Models {
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using Microsoft.AspNet.Identity.EntityFramework;
public partial class CarbonEDM : IdentityDbContext<ApplicationUser> {
public CarbonEDM()
: base("name=CarbonDB") {
}
public static CarbonEDM Create() {
return new CarbonEDM();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>().ToTable("Users", "dbo");
modelBuilder.Entity<IdentityRole>().ToTable("Roles", "dbo");
modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles", "dbo");
modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims", "dbo");
modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins", "dbo");
}
}
}
您必须继承自 IdentityUser class。 (您是否有继承自 IdentityUser 的自定义用户 class?)。如果是这种情况,请不要覆盖 IdentityUser 的 table 名称,而只覆盖您的自定义用户 class。假设您的自定义用户 class 是 ApplicationUser:
modelBuilder.Entity<ApplicationUser>().ToTable("Users");
删除或评论以下行
//modelBuilder.Entity<IdentityUser>().ToTable("Users", "dbo");