2016-12-03 74 views
0

我試圖用一個「名稱」延長asp.net用戶身份屬性,因此我跟着這個帖子How to extend available properties of User.Identity延伸asp.net用戶身份的原因數據庫錯誤

的描述,但我這樣做,並試圖後登錄我得到這個錯誤「自從創建數據庫以來,支持'ApplicationDbContext'上下文的模型已經發生了變化。考慮使用Code First Migrations來更新數據庫」

我可以修復這個問題還是隻能擴展asp.net ueser第一次創建數據庫之前的身份?

回答

0
Based on Asp.Net template database will generate the bellow structure: 

enter image description here

In order to extend asp.net identity user and keep the things simple, please update IdentityModels.cs with following code: 

//CODE 

    using System.Data.Entity; 
    using System.Security.Claims; 
    using System.Threading.Tasks; 
    using Microsoft.AspNet.Identity; 
    using Microsoft.AspNet.Identity.EntityFramework; 
    using System.Data.Entity.Migrations; 

namespace WebApplication.Models 
{ 
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more. 
    public class ApplicationUser : IdentityUser 
    { 
     public string Name { get; set; } 
     public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) 
     { 
      // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType 
      var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 
      // Add custom user claims here 
      return userIdentity; 
     } 
    } 

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
    { 
     public ApplicationDbContext() 
      : base("DefaultConnection", throwIfV1Schema: false) 
     { 
      Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, ApplicationDbContextConfiguration>()); 
     } 

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      base.OnModelCreating(modelBuilder); 
      modelBuilder.Entity<IdentityRole>().ToTable("AspNetRoles"); 
      modelBuilder.Entity<IdentityUserRole>().ToTable("AspNetUserRoles"); 
      modelBuilder.Entity<IdentityUserLogin>().ToTable("AspNetUserLogins"); 
      modelBuilder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaims"); 
      modelBuilder.Entity<ApplicationUser>().ToTable("AspNetUsers"); 
     } 


     public static ApplicationDbContext Create() 
     { 
      return new ApplicationDbContext(); 
     } 
    } 



    internal sealed class ApplicationDbContextConfiguration : DbMigrationsConfiguration<ApplicationDbContext> 
    { 
     public ApplicationDbContextConfiguration() 
     { 

      ContextKey = "WebApplication.Models.ApplicationDbContext"; //Retrieved from the database table dbo.__MigrationHistory 

#if DEBUG 
      AutomaticMigrationsEnabled = true; 
      AutomaticMigrationDataLossAllowed = true; 
#else 
       AutomaticMigrationsEnabled = false; 
       AutomaticMigrationDataLossAllowed = false; 

#endif 
     } 

     protected override void Seed(ApplicationDbContext context) 
     { 
      base.Seed(context); 
     } 
    } 
} 


The output is: 

enter image description here

PS: You can rename default names for asp.net generated tables, personally I prefer to remove AspNet suffix 
+0

對不起,我試圖自動建議。這沒有幫助。它給出了相同的錯誤.. – MTplus

+0

@Mtplus,我編輯了我的答案,我創建了您的需求的代碼,只需複製粘貼到您的項目,你應該已經修復了你的問題 –

+0

嗨,不知道你的建議會給我相同的結果。但是,繼承人我做了那些工作..1。啓用遷移2.添加遷移AddedNameField 3.更新數據庫。 – MTplus