Identity 2.0 Code First Table Renaming

2019-03-02 06:36发布

问题:

I'm trying to rename the identity tables to Roles, Users, UserRoles, UserLogins and UserClaims. I partially succeeded doing and using the command Update-Table updates my database.

However I cant seem to be able to get rid off the AspNetUsers table, it always gets generated with only one column, the Id column, although I get another Users table with the full list of columns, and another Id column.

The script generated by 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])
)

And my db context:

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:

You must be inheriting from the IdentityUser class. (Do you have a custom user class that inherits from IdentityUser?). If that's the case, don't overwrite the table name for IdentityUser, but only for your custom user class. Assuming that your custom user class is ApplicationUser:

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

delete or comment the following line

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