我正在尝试将标识表重命名为Roles,Users,UserRoles,UserLogins和UserClaims。我部分成功地完成了并使用命令更新表更新我的数据库。
但是,我似乎无法摆脱 AspNetUsers 表,它总是只用一列生成,即 Id 列,尽管我得到了另一个包含完整列列表的用户表和另一个 Id 列。
由更新数据库生成的脚本
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 类
继承。 (您是否有从 IdentityUser 继承的自定义用户类?)。如果是这种情况,请不要覆盖 IdentityUser 的表名,而只覆盖自定义用户类的表名。假设您的自定义用户类是应用程序用户:
modelBuilder.Entity<ApplicationUser>().ToTable("Users");
删除或注释以下行
//modelBuilder.Entity<IdentityUser>().ToTable("Users", "dbo");