身份2.0 code首先重命名表 [英] Identity 2.0 Code First Table Renaming

查看:176
本文介绍了身份2.0 code首先重命名表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图重新命名的身份表,角色,用户的UserRole,UserLogins和UserClaims。我部分成功做并且使用命令更新表更新我的数据库。

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.

不过,我不能似乎能够摆脱掉AspNetUsers表,它总是被只有一列,Id列生成的,虽然我得到其他用户的表列的完整列表,另一个ID列。

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])
)

和我的数据库方面:

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覆盖表名称,但仅用于自定义用户类。假设您的自定义用户类是ApplicationUser:

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

这篇关于身份2.0 code首先重命名表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆