Identity 2.0 Code First Table重命名

时间:2015-01-19 01:15:46

标签: asp.net asp.net-identity asp.net-identity-2

我尝试将身份表重命名为Roles,Users,UserRoles,UserLogins和UserClaims。我部分成功并使用命令Update-Table更新我的数据库。

但是我似乎无法摆脱AspNetUsers表,它总是只生成一列Id列,虽然我得到另一个包含完整列列表的Users表,以及另一个Id列。 / p>

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");

    }
}

}

1 个答案:

答案 0 :(得分:4)

您必须继承IdentityUser类。 (您是否有一个继承自IdentityUser的自定义用户类?)。如果是这种情况,请不要覆盖IdentityUser的表名,而只覆盖自定义用户类。假设您的自定义用户类是ApplicationUser:

modelBuilder.Entity<ApplicationUser>().ToTable("Users");

删除或评论以下行

//modelBuilder.Entity<IdentityUser>().ToTable("Users", "dbo");