2017-03-16 60 views
1

我創建了一個數據庫上下文。我想刪除表格AspNetUsers中的一些列,例如PhoneNumber,PhoneNumberConfirmed,因爲它在我的應用程序中不是必需的。從AspNetUsers中刪除列

namespace TestTest.Infrastuctures 
{ 
    public class TestContext:IdentityDbContext<ApplicationUser> 
    { 
     public TestContext() 
      : base("TestContext") 
     { 

     } 
     public static TestContext Create() 
     { 
      return new TestContext(); 
     } 
     public virtual DbSet<Category> Categories { get; set; } 
     public virtual DbSet<Product> Products { get; set; } 

    } 

    public class ApplicationUserEntityTypeConfiguration : EntityTypeConfiguration<ApplicationUser> 
    { 
     public ApplicationUserEntityTypeConfiguration() 
     { 
      Ignore(p => p.PhoneNumber); 
      Ignore(p => p.PhoneNumberConfirmed); 
      Ignore(p => p.EmailConfirmed); 
      Ignore(p => p.TwoFactorEnabled); 

     } 
    } 
} 

代碼工作正常,但沒有刪除AspNetUsers中的選定列。 enter image description here

歡迎任何幫助或建議。

更新

此代碼解決我的問題:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 

      var user = modelBuilder.Entity<ApplicationUser>(); 
      user.Ignore(u => u.EmailConfirmed); 
      user.Ignore(u => u.PhoneNumber); 
      user.Ignore(u => u.PhoneNumberConfirmed); 

      var identityUserRole = modelBuilder.Entity<IdentityUserRole>(); 
      identityUserRole.HasKey(r => new { r.UserId, r.RoleId }); 

      var identityUserLogin = modelBuilder.Entity<IdentityUserLogin>(); 
      identityUserLogin.HasKey(l => new { l.LoginProvider, l.ProviderKey, l.UserId }); 

      var claims = modelBuilder.Entity<IdentityUserClaim>(); 

     } 
+0

你的更新基本上是我的答案所說的。 – DavidG

回答

2

我相信你能做到這一點,但不一定與EntityTypeConfiguration方法。相反,嘗試重寫OnModelCreating方法在上下文:

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

    modelBuilder.Entity<IdentityUser>() 
     .Ignore(p => p.PhoneNumber) 
     .Ignore(p => p.PhoneNumberConfirmed) 
     .Ignore(p => p.EmailConfirmed) 
     .Ignore(p => p.TwoFactorEnabled);  
} 
0

轉至類ApplicationUser,刪除******中國,PhoneNumberConfirmed屬性。

添加遷移,然後更新您的數據庫。

+0

這些屬性來自Identity框架中的一個類,如果不從Githugb下載整個東西並自行編譯它,則不可編輯。感謝大衛 – DavidG

+2

,你的回答也幫助了我 –