如何清除dbo.AspNetUserClaims和dbo.AspNetUserLogins表(IdentityUserClaim和IdentityUserLogin实体)? [英] How to remove dbo.AspNetUserClaims and dbo.AspNetUserLogins tables (IdentityUserClaim and IdentityUserLogin entities)?
问题描述
我们的应用程序不需要身份使用的过多登录和声明功能。如果这些表simlpy没有在数据库中创建,这将是很好的,但我不想重新实现所有身份类...
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...
我' d假定它是像
public ApplicationDbContext : IdentityDbContext
{
[...]
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Ignore<IdentityUserClaim>();
modelBuilder.Ignore<IdentityUserLogin>();
}
}
你会认为这是根据功能描述,但它没有。 AspNetUserClaim
和 AspNetUserLogins
表仍然创建。
You would think this works according to the function descriptions, but it doesn't. The AspNetUserClaim
and AspNetUserLogins
tables still get created.
推荐答案
这是一个 ApplicationDbContext
的 OnModelCreating
方法为您的案例。实际上它只是 IdentityDbContext
的 OnModelCreating
方法,忽略 IdentityUserClaim
和 IdentityUserLogin
实体。
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.
请注意, OnModelCreating
override不应该调用 base.OnModelCreating
方法。
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>();
}
}
以下是相关的代码行:
user.Ignore(u => u.Claims);
user.Ignore(u => u.Logins);
modelBuilder.Ignore<IdentityUserLogin>();
modelBuilder.Ignore<IdentityUserClaim>();
根据需要,将导致以下迁移,而不需要 dbo.AspNetUserClaims
和 dbo.AspNetUserLogins
表:
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");
这篇关于如何清除dbo.AspNetUserClaims和dbo.AspNetUserLogins表(IdentityUserClaim和IdentityUserLogin实体)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!