0

I tried to extend the asp.net identity user with a "Name" property, I therefore followed the description in this post How to extend available properties of User.Identity

But after I did that and tried to login I get this error "The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database"

Can I fix this or can you only extend the asp.net ueser identity before the database is created the first time?

Community
  • 1
  • 1
MTplus
  • 2,077
  • 4
  • 34
  • 51

1 Answers1

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 
Ionut N
  • 434
  • 4
  • 9
  • Sorry I tried the automatic suggestion. That did not help. It give the same error.. – MTplus Dec 03 '16 at 19:09
  • @Mtplus, I edited my answer and I created the code for your needs, just copy paste in your project and you should have fixed your issue – Ionut N Dec 04 '16 at 10:44
  • Hi, don't know if your suggestion would give me the same result. But heres what I did that worked..1. enable-migrations 2. add-migration AddedNameField 3. Update-database. – MTplus Dec 04 '16 at 11:17