2014-08-29 118 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