ASP.NET标识从AspNetUsers表中删除列

exe*_*xeq 12 c# asp.net entity-framework asp.net-identity

当我使用ASP.NET Identity第一个代码方法时,我想以自己的方式在AspNetUsers表中生成列.我不需要存储多个具有空值的列.我只需要列Id,SecurityStamp和UserName.我发现只有帖子在这里:AspNet Identity 2.0电子邮件和用户名重复,但它仍然没有被发现(由于Santosh评论中的错误).

所以有人能告诉我如何解决这个问题吗?

编辑:甚至可以删除其中一些列/属性?

谢谢

Aas*_*mar 30

实际上你可以忽略这些字段,只需要在上下文类中配置你的实体OnModelCreating:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<IdentityUser>().Ignore(c => c.AccessFailedCount)
                                           .Ignore(c=> c.LockoutEnabled)
                                           .Ignore(c=>c.LockoutEndDateUtc)
                                           .Ignore(c=>c.Roles)
                                           .Ignore(c=>c.TwoFactorEnabled);//and so on...

        modelBuilder.Entity<IdentityUser>().ToTable("Users");//to change the name of table.

}
Run Code Online (Sandbox Code Playgroud)


小智 5

实际上,您可以在OnModelCreating上下文类中配置您的实体.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<IdentityUser>().Ignore(u => u.AccessFailedCount);
    //and so on...
}
Run Code Online (Sandbox Code Playgroud)

或者,如果您的应用程序为每个配置都有一个单独的文件(我建议使用),您可以这样做:

public class ApplicationUserEntityTypeConfiguration : EntityTypeConfiguration<ApplicationUser>
{
    public ApplicationUserEntityTypeConfiguration()
    {
        Ignore(p => p.AccessFailedCount);
        //And so on..
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 当我要添加迁移时,它会抛出异常 (2认同)

drn*_*eel 3

简短的回答是否定的,除非您自己实施。或者您可以等待他们在codeplex上开源 asp.net 身份。谁知道这需要多长时间。

默认实现包括所有未使用的列(见下文)。

// Summary:
//     Default EntityFramework IUser implementation
//
// Type parameters:
//   TKey:
//
//   TLogin:
//
//   TRole:
//
//   TClaim:
public class IdentityUser<TKey, TLogin, TRole, TClaim> : IUser<TKey>
    where TLogin : Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin<TKey>
    where TRole : Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole<TKey>
    where TClaim : Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim<TKey>
{
    // Summary:
    //     Constructor
    public IdentityUser();

    // Summary:
    //     Used to record failures for the purposes of lockout
    public virtual int AccessFailedCount { get; set; }
    //
    // Summary:
    //     Navigation property for user claims
    public virtual ICollection<TClaim> Claims { get; }
    //
    // Summary:
    //     Email
    public virtual string Email { get; set; }
    //
    // Summary:
    //     True if the email is confirmed, default is false
    public virtual bool EmailConfirmed { get; set; }
    //
    // Summary:
    //     User ID (Primary Key)
    public virtual TKey Id { get; set; }
    //
    // Summary:
    //     Is lockout enabled for this user
    public virtual bool LockoutEnabled { get; set; }
    //
    // Summary:
    //     DateTime in UTC when lockout ends, any time in the past is considered not
    //     locked out.
    public virtual DateTime? LockoutEndDateUtc { get; set; }
    //
    // Summary:
    //     Navigation property for user logins
    public virtual ICollection<TLogin> Logins { get; }
    //
    // Summary:
    //     The salted/hashed form of the user password
    public virtual string PasswordHash { get; set; }
    //
    // Summary:
    //     PhoneNumber for the user
    public virtual string PhoneNumber { get; set; }
    //
    // Summary:
    //     True if the phone number is confirmed, default is false
    public virtual bool PhoneNumberConfirmed { get; set; }
    //
    // Summary:
    //     Navigation property for user roles
    public virtual ICollection<TRole> Roles { get; }
    //
    // Summary:
    //     A random value that should change whenever a users credentials have changed
    //     (password changed, login removed)
    public virtual string SecurityStamp { get; set; }
    //
    // Summary:
    //     Is two factor enabled for the user
    public virtual bool TwoFactorEnabled { get; set; }
    //
    // Summary:
    //     User name
    public virtual string UserName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)