如何在ASP.NET Core 2.2中删除一些默认身份表

时间:2019-01-16 05:18:29

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

我试图通过忽略OnModelCreating中的某些默认身份表来删除它们,如下所示:

public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<ApplicationUser>(p => p.HasOne(e=>e.Department).WithOne().IsRequired());
        modelBuilder.Ignore<IdentityUserToken<string>>();
        modelBuilder.Ignore<IdentityUserClaim<string>>();
        modelBuilder.Ignore<IdentityUserLogin<string>>();
        modelBuilder.Ignore<IdentityRoleClaim<string>>();
        modelBuilder.Ignore<IdentityUser<string>>();
    }
}

在startup.cs中,我将身份配置为:

services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddDefaultUI(UIFramework.Bootstrap4)
            .AddEntityFrameworkStores<ApplicationDbContext>();

当我注册用户时,一切正常,但是当用户尝试登录时,抛出此错误:

  

无法为“ IdentityUserClaim”创建DbSet,因为这   类型不包含在上下文模型中

我在配置中错过了什么?

请不要将其标记为重复项,因为我进行了大量搜索,但无法找到合适的解决方案

2 个答案:

答案 0 :(得分:0)

好吧!我对这个问题进行了深入研究!结论是您不能删除这些表。如果删除这些表,则标识将无法按预期工作。有关更多详细信息:

How to remove the role-related tables from ASP.NET Identity Core 2.0

Similar issue posted on Github

答案 1 :(得分:0)

我按照以下方式进行操作,对我有用。我忽略了一些ASP身份表以及AspNetUser中我的方案所不需要的列 我正在使用ASP .Net Core 2.2

public class ApplicationContext: IdentityDbContext
{
    public ApplicationContext(DbContextOptions options): base(options)
    {

    }

    public DbSet<ApplicationUser> ApplicationUsers { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Ignore<IdentityRole>();
        modelBuilder.Ignore<IdentityUserToken<string>>();
        modelBuilder.Ignore<IdentityUserRole<string>>();
        modelBuilder.Ignore<IdentityUserLogin<string>>();
        modelBuilder.Ignore<IdentityUserClaim<string>>();
        modelBuilder.Ignore<IdentityRoleClaim<string>>();
        modelBuilder.Entity<IdentityUser>()

            .Ignore(c => c.AccessFailedCount)
            .Ignore(c => c.LockoutEnabled)
            .Ignore(c => c.TwoFactorEnabled)
            .Ignore(c => c.ConcurrencyStamp)
            .Ignore(c => c.LockoutEnd)
            .Ignore(c => c.EmailConfirmed)
            .Ignore(c => c.TwoFactorEnabled)
            .Ignore(c => c.LockoutEnd)
            .Ignore(c => c.AccessFailedCount)
            .Ignore(c => c.PhoneNumberConfirmed)
            .Ignore(c => c.Email)
            .Ignore(c => c.NormalizedEmail)
            .Ignore(c => c.PhoneNumber);



        modelBuilder.Entity<IdentityUser>().ToTable("Users");//to change the name of table.

    } }}