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

时间:2015-03-09 17:36:28

标签: c# asp.net-mvc entity-framework asp.net-identity

我们的申请并不需要过多的"登录"和"索赔"身份使用的功能。如果这些表格没有在数据库中创建,那将会很好,但我不想重新实现所有的身份类......

我认为它类似

public ApplicationDbContext : IdentityDbContext
{
        [...]

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

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

你会认为这根据功能描述有效,但它没有。 <{1}}和AspNetUserClaim表仍然会被创建。

正确的方法是什么?

1 个答案:

答案 0 :(得分:12)

以下是针对您的案例的ApplicationDbContext OnModelCreating方法的实现。实际上它只是忽略IdentityDbContextOnModelCreating个实体的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.AspNetUserClaimsdbo.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");