【问题标题】:How to remove some default identity tables in ASP.NET Core 2.2如何删除 ASP.NET Core 2.2 中的一些默认标识表
【发布时间】:2019-01-16 05:18:29
【问题描述】:

我正在尝试通过忽略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,因为这 上下文模型中不包含类型

我在配置中遗漏了什么?

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

【问题讨论】:

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


    【解决方案1】:

    好的!我已经深入研究了这个问题!结论是你不能删除这些表。如果您删除这些表,则身份将无法按预期工作。更多详情:

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

    Similar issue posted on Github

    【讨论】:

    • 生成和创建表格没有问题!!!正如我所说,一切正常,除非用户想要登录它会引发上述错误
    • @ElyasEsna 请检查我的答案!我已经更新了我的答案!
    • 这很奇怪,我认为在 2.0 版本中是可能的!
    • 这里有一篇文章。这可能在 ASP.NET Core 1.1 上,但这是疯狂的工作量。我没有测试它是否真的有效。 medium.com/@nativoplus/asp-net-core-identity-3-0-6018fc151b4
    【解决方案2】:

    我通过以下方式做到了这一点,它对我有用。我忽略了一些 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.
    
        } }}
    

    【讨论】:

      猜你喜欢
      • 2018-12-19
      • 1970-01-01
      • 2019-06-25
      • 1970-01-01
      • 2019-08-17
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 2019-12-18
      相关资源
      最近更新 更多