在ASP.NET Core MVC6中更改Identity 3.0表名称不起作用 [英] Changing Identity 3.0 table names in ASP.NET Core MVC6 not working

查看:86
本文介绍了在ASP.NET Core MVC6中更改Identity 3.0表名称不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 相同的问题被询问,但12天后未得到答复...

  1. This same question was asked and has not been answered after 12 days...

我看过

I have looked at this which uses "ToTable" as an update to the question.

我查看了,它似乎已过时.

I have looked at this which appears to be out of date.

我想更改标识3.0表的表名-ASP.NET Core.

I want to change the table names of the identity 3.0 tables - ASP.NET Core.

到目前为止,使用"ToTable"选项的更新(上述第2个),我设法破坏了我的锁定文件,结果破坏了该项目.第三种选择已过期.

So far, using the "ToTable" option's (No. 2 above) update, I have managed to corrupt my lockfile and have as a result corrupted the project. The third option is out of date.

我创建了一个普通项目-没有通过VS2015以标识3.0创建的任何更改

I created a vanilla project - no changes just created via VS2015 with identity 3.0

然后我尝试了这个:

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);

        builder.Entity<IdentityUser>().ToTable("MyUsers").Property(p => p.Id).HasColumnName("UserId");
        builder.Entity<ApplicationUser>().ToTable("MyUsers").Property(p => p.Id).HasColumnName("UserId");
        builder.Entity<IdentityRole>().ToTable("MyRoles");
    }

然后我检查了更新的迁移,以查看表名称是否已更改,并且它们没有.

I then checked the updated migration to see if the table names had changed and they havent.

此刻我正在使用1.0.0-rc1-update2.

I am using 1.0.0-rc1-update2 at the moment.

如何更改这些表的名称?

How do you change the names of these tables?

推荐答案

尝试此代码,它将所有asp.net标识默认表名更改为自定义表名,例如用户,角色,UserClaim ...等.它对我有用.

Try this code, it changes all asp.net identify default table names to custom table names e.g. User, Role, UserClaim ... etc. it works for me.

using Microsoft.AspNetCore.Identity;
...
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{

     ...

     protected override void OnModelCreating(ModelBuilder modelBuilder)
     {
        base.OnModelCreating(modelBuilder);
        // Add your customizations after calling base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<ApplicationUser>().ToTable("User");
        modelBuilder.Entity<IdentityRole>().ToTable("Role");
        modelBuilder.Entity<IdentityUserClaim<string>>().ToTable("UserClaim");
        modelBuilder.Entity<IdentityUserRole<string>>().ToTable("UserRole");
        modelBuilder.Entity<IdentityUserLogin<string>>().ToTable("UserLogin");
        modelBuilder.Entity<IdentityRoleClaim<string>>().ToTable("RoleClaim");
        modelBuilder.Entity<IdentityUserToken<string>>().ToTable("UserToken");
      }

}

这篇关于在ASP.NET Core MVC6中更改Identity 3.0表名称不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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