How to delete dbo.AspNetUserClaims and dbo.AspNetUserLogins tables (IdentityUserClaim and IdentityUserLogin objects)?

Our application does not need excessive functionality of the “Logins” and “Claims” that the identifier uses. It would be nice if these simlpy tables were not created in the database, but I do not want to override all identification classes ...

I would suggest that this is something like

public ApplicationDbContext : IdentityDbContext { [...] protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Ignore<IdentityUserClaim>(); modelBuilder.Ignore<IdentityUserLogin>(); } } 

You think this works according to function descriptions, but it doesn’t. AspNetUserClaim and AspNetUserLogins tables are still being created.

What is the right way to do this?

+6
source share
1 answer

Here is the implementation of the ApplicationDbContext OnModelCreating method for your case. This is actually just an IdentityDbContext OnModelCreating method, ignoring IdentityUserClaim and IdentityUserLogin objects.

Note that overriding OnModelCreating should not call the 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>(); 

If necessary, this will lead to the following migration without the dbo.AspNetUserClaims and dbo.AspNetUserLogins :

 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"); 
+10
source

Source: https://habr.com/ru/post/983544/


All Articles