How to remove dbo.AspNetUserClaims and dbo.AspNetUserLogins tables (IdentityUserClaim and IdentityUserLogin entities)?

孤人 提交于 2019-12-21 12:17:28

问题


Our application doesn't need the excessive "Logins" and "Claims" functionality that identity uses. It would be nice if these tables simlpy didn't get created in the database, but I don't want to have to reimplement all identity classes...

I'd assume it's something like

public ApplicationDbContext : IdentityDbContext
{
        [...]

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

            modelBuilder.Ignore<IdentityUserClaim>();
            modelBuilder.Ignore<IdentityUserLogin>();
        }
}

You would think this works according to the function descriptions, but it doesn't. The AspNetUserClaim and AspNetUserLogins tables still get created.

What's the right way to do this?


回答1:


Here is an implementation of ApplicationDbContext's OnModelCreating method for your case. In fact it's just IdentityDbContext's OnModelCreating method with ignoring of IdentityUserClaim and IdentityUserLogin entities.

Note that OnModelCreating override should not invoke base.OnModelCreating method.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // Needed to ensure subclasses share the same table
        var user = modelBuilder.Entity<ApplicationUser>()
            .ToTable("AspNetUsers");
        user.HasMany(u => u.Roles).WithRequired().HasForeignKey(ur => ur.UserId);
        user.Ignore(u => u.Claims);
        user.Ignore(u => u.Logins);
        user.Property(u => u.UserName)
            .IsRequired()
            .HasMaxLength(256)
            .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("UserNameIndex") { IsUnique = true }));

        // CONSIDER: u.Email is Required if set on options?
        user.Property(u => u.Email).HasMaxLength(256);

        modelBuilder.Entity<IdentityUserRole>()
            .HasKey(r => new { r.UserId, r.RoleId })
            .ToTable("AspNetUserRoles");

        var role = modelBuilder.Entity<IdentityRole>()
            .ToTable("AspNetRoles");
        role.Property(r => r.Name)
            .IsRequired()
            .HasMaxLength(256)
            .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("RoleNameIndex") { IsUnique = true }));
        role.HasMany(r => r.Users).WithRequired().HasForeignKey(ur => ur.RoleId);

        modelBuilder.Ignore<IdentityUserLogin>();
        modelBuilder.Ignore<IdentityUserClaim>();
    }
}

Here are the relevant lines of code:

user.Ignore(u => u.Claims);
user.Ignore(u => u.Logins);
modelBuilder.Ignore<IdentityUserLogin>();
modelBuilder.Ignore<IdentityUserClaim>();

As required, it would result in the following migration without dbo.AspNetUserClaims and dbo.AspNetUserLogins tables:

CreateTable(
    "dbo.AspNetRoles",
    c => new
        {
            Id = c.String(nullable: false, maxLength: 128),
            Name = c.String(nullable: false, maxLength: 256),
        })
    .PrimaryKey(t => t.Id)
    .Index(t => t.Name, unique: true, name: "RoleNameIndex");

CreateTable(
    "dbo.AspNetUserRoles",
    c => new
        {
            UserId = c.String(nullable: false, maxLength: 128),
            RoleId = c.String(nullable: false, maxLength: 128),
        })
    .PrimaryKey(t => new { t.UserId, t.RoleId })
    .ForeignKey("dbo.AspNetRoles", t => t.RoleId, cascadeDelete: true)
    .ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
    .Index(t => t.UserId)
    .Index(t => t.RoleId);

CreateTable(
    "dbo.AspNetUsers",
    c => new
        {
            Id = c.String(nullable: false, maxLength: 128),
            Email = c.String(maxLength: 256),
            EmailConfirmed = c.Boolean(nullable: false),
            PasswordHash = c.String(),
            SecurityStamp = c.String(),
            PhoneNumber = c.String(),
            PhoneNumberConfirmed = c.Boolean(nullable: false),
            TwoFactorEnabled = c.Boolean(nullable: false),
            LockoutEndDateUtc = c.DateTime(),
            LockoutEnabled = c.Boolean(nullable: false),
            AccessFailedCount = c.Int(nullable: false),
            UserName = c.String(nullable: false, maxLength: 256),
        })
    .PrimaryKey(t => t.Id)
    .Index(t => t.UserName, unique: true, name: "UserNameIndex");


来源:https://stackoverflow.com/questions/28948309/how-to-remove-dbo-aspnetuserclaims-and-dbo-aspnetuserlogins-tables-identityuser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!