【问题标题】:ASP.NET Identity remove column from AspNetUsers tableASP.NET Identity 从 AspNetUsers 表中删除列
【发布时间】:2014-10-22 23:16:26
【问题描述】:

当我使用 ASP.NET Identity first code 方法时,我想以我自己的方式在 AspNetUsers 表中生成列。我不需要存储多个具有空值的列。我只需要列 Id、SecurityStamp 和 UserName。 我发现的唯一帖子在这里:AspNet Identity 2.0 Email and UserName duplication,但它仍然不受欢迎(由于 Santosh 评论中的错误)。

那么谁能告诉我如何解决这个问题?

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

谢谢

【问题讨论】:

  • 您可能不需要这些值,但您确定 asp.net 不需要它们才能正常工作吗?
  • 我意识到可能存在一些关系,我已经编辑了我的问题
  • 如果您想要自己的自定义表格,请不要使用Identity.Entityframework
  • 您没有用户的电子邮件吗?密码哈希呢?为什么空列困扰您?它们不占用太多空间,对数据库性能没有影响。你只是在为自己创造额外的工作。
  • 谢谢你,这正是我想听到的

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


【解决方案1】:

其实你可以忽略字段,只需要在你的上下文类中配置你的实体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.

}

【讨论】:

  • 您能否更具体地了解“context”类的位置?
  • 要回答我自己的问题,上下文类是 ApplicationDbContext
【解决方案2】:

其实可以,只要在上下文类的OnModelCreating 上配置实体即可。

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

或者,如果您的应用程序对每个配置都有一个单独的文件(这是我推荐的),您可以这样做:

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

【讨论】:

  • 我要添加迁移时抛出异常
【解决方案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; }
}

【讨论】:

  • 谢谢。您是否认为,在此表中包含 100 000 条甚至更多记录且所有这些字段都为 null 是否存在问题?我不想创建自定义表,因为我正在使用 ASP.NET Identity 登录功能。
  • 无论如何我都不是 DBA,但我认为这不是什么大问题。这些表已正确编入索引和键控,因此您应该没问题。另一种选择可能是考虑这个项目:github.com/brockallen/BrockAllen.IdentityReboot。我没有亲自使用过它,但它可能会给你你想要的灵活性。
【解决方案4】:

我知道这可能并不完全相关,但如果您只是想排除 JSON 响应中的列,您只需将 [JsonIgnore] 放在属性上方即可。我使用实体,因此默认情况下(加密的)密码包含在模型中。即使密码已加密,您仍然不希望最终用户获得它。保持对该属性的访问而不将其包含在响应中的一种方法如下所示。

在下面的示例中,Password 字段将从 Json 响应中删除,因为我们将 [JsonIgnore] 添加到模型中。

public int Id { get; set; }
public string Email { get; set; }

[JsonIgnore]
public string Password { get; set; } // <--- Removed from JSON response

public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
public bool Active { get; set; }

这是一个示例 JSON 响应。

{
  "id": 1,
  "email": "ddavis@example.com",
  "firstName": "Daniel",
  "middleName": "Cool-Guy",
  "lastName": "Davis",
  "phoneNumber": "12055550000",
  "active": true
}

【讨论】:

    【解决方案5】:

    您可以创建 IdentityUser 的后代覆盖您需要的属性并装饰它们的 [NotMapped] 属性。然后创建(重新创建)身份表。

    [NotMapped]
    public override bool EmailConfirmed { get; set; }
    
    

    【讨论】:

    • 我没试过,但如果可行,这似乎是解决方案。
    猜你喜欢
    • 1970-01-01
    • 2014-05-14
    • 2021-07-15
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 2019-09-01
    • 2014-09-11
    相关资源
    最近更新 更多