2016-12-03 1 views
0

J'ai essayé d'étendre l'utilisateur d'identité asp.net avec une propriété « Name », je donc suivi la description de ce poste How to extend available properties of User.Identityétendent l'erreur de base de données de la cause de l'utilisateur d'identité asp.net

Mais après que je l'ai fait et essayé de Je reçois cette erreur "Le modèle supportant le contexte 'ApplicationDbContext' a changé depuis la création de la base de données."

Puis-je résoudre ce problème ou ne pouvez-vous étendre que le serveur asp.net? identité avant la création de la base de données la première fois?

Répondre

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

Désolé j'ai essayé la suggestion automatique. Cela n'a pas aidé. Il donne la même erreur .. – MTplus

+0

@Mtplus, j'ai édité ma réponse et j'ai créé le code pour vos besoins, il suffit de copier coller dans votre projet et vous auriez dû corriger votre problème –

+0

Salut, je ne sais pas si votre suggestion me donnerait le même résultat. Mais voici ce que j'ai fait qui a fonctionné. enable-migrations 2. ajout-migration AddedNameField 3. Mise à jour-base de données. – MTplus