c# – 如何删除dbo.AspNetUserClaims和dbo.AspNetUserLogins表(IdentityUserClaim和IdentityUserLogin实体)?

前端之家收集整理的这篇文章主要介绍了c# – 如何删除dbo.AspNetUserClaims和dbo.AspNetUserLogins表(IdentityUserClaim和IdentityUserLogin实体)?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们的应用程序不需要身份使用的过多“登录”和“声明”功能.如果没有在数据库中创建这些表格simlpy会很好,但我不想重新实现所有的身份类…

我假设它是这样的

public ApplicationDbContext : IdentityDbContext
{
        [...]

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

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

您会认为这根据功能描述有效,但事实并非如此.仍然会创建AspNetUserClaim和AspNetUserLogins表.

这样做的正确方法是什么?

解决方法

这是针对您的案例的ApplicationDbContext的OnModelCreating方法的实现.事实上,它只是IdentityDbContext的OnModelCreating方法,忽略了IdentityUserClaim和IdentityUserLogin实体.

请注意,OnModelCreating覆盖不应调用base.OnModelCreating方法.

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表,将导致以下迁移:

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,RoleId = c.String(nullable: false,})
    .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",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,})
    .PrimaryKey(t => t.Id)
    .Index(t => t.UserName,name: "UserNameIndex");

猜你在找的C#相关文章