2014-08-29 54 views
6

当我使用ASP.NET身份第一代码方法时,我想以自己的方式在AspNetUsers表中生成列。我不需要存储具有空值的多个列。我只需要列ID,SecurityStamp和用户名。 只发布,我发现在这里:AspNet Identity 2.0 Email and UserName duplication,但它仍然不受欢迎(由于桑托什评论中的错误)。ASP.NET身份从AspNetUsers表中删除列

那么有人可以告诉我如何解决这个问题吗?

编辑:甚至有可能删除这些列/属性中的一些?

谢谢

+3

你可能不需要这些VAL你是否确定asp.net不需要它们正常工作? – 2014-08-29 00:17:43

+0

我意识到可以有一些关系,我已经编辑我的问题 – exeq 2014-08-29 00:29:41

+1

如果你想要自己定制表格,那就不要使用Identity.Entityframework' – Shoe 2014-08-29 00:54:56

回答

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; } 
} 
+0

谢谢。你是否认为,在这个表中有100000条记录,甚至更多,所有这些字段都是空值是个问题?我不想创建自定义表,因为我使用的是ASP.NET身份登录功能。 – exeq 2014-08-29 08:32:28

+0

我不是一个DBA,但我不认为这是一个大问题。表格索引和键入正确,所以你应该没问题。另一种选择可能是考虑这个项目:https://github.com/brockallen/BrockAllen.IdentityReboot。我没有亲自使用它,但它可能会给你你想要的灵活性。 – drneel 2014-08-29 11:32:11

3

其实你可以,只需在你的上下文类的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.. 
    } 
} 
+1

当我要添加迁移时抛出异常 – 2016-07-01 12:50:28

16

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

} 
+0

您可以更具体地了解“上下文”类的位置吗? – webworm 2017-02-01 22:27:08

+1

要回答我自己的问题,上下文类是ApplicationDbContext – webworm 2017-02-01 22:28:45

+0

@webworm是的你是对的。 – 2017-02-02 10:03:10