Upgrading to ASP.NET Identity 2 - Then Fix Errors

I am working on a MVC5 project but a lot of libraries it has referenced were outdated. Therefore I just opened up "Managed Nuget Pkgs For Solution" and clicked on update all button. It did update all and then I was so happy, i built the project, and ran it.

Lucky Me! I got these errors. Sounds familiar?

The model backing the 'ApplicationDbContext' context has changed since the database was created. This could have happened because the model used by ASP.NET Identity Framework has changed or the model being used in your application has changed. To resolve this issue, you need to update your database. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=301867).  Before you update your database using Code First Migrations, please disable the schema consistency check for ASP.NET Identity by setting throwIfV1Schema = false in the constructor of your ApplicationDbContext in your application.       public ApplicationDbContext() : base("ApplicationServices", throwIfV1Schema:false)

---------------------- fixed the above, got the below error

One or more validation errors were detected during model generation:
Oklo.SmartTravel.Web.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.Oklo.SmartTravel.Web.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.

------------------------- fixed the above, got the below error

Invalid column name 'UserId'

----------------------------- fixed the above, it is working now

Then I read a lot of SO threads, tried different solutions, somehow it started working.

I can't use migrations on the database im using so any changes I do had to come from my code. I have no permission to change any of the database tables.

Solutions to these issues:

1. Goto Global.asax, add this line of code to Application_Start()

 Database.SetInitializer<ApplicationDbContext>(null);

"ApplicationDbContext" is the name of your database context class.

2. then change the ApplicationDbContext like the following code

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base(EnvironmentProfile.Current.GetValue("Security", "connectionString"), throwIfV1Schema: false)
        {
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            var userClaims = modelBuilder.Entity<IdentityUserClaim>()
                .ToTable("AspNetUserClaims");
            userClaims.Property(u => u.UserId).HasColumnName("User_Id");
        }
    }

What this code does is, mapping our new Identity2 'UserId' property to the AspNetUserClaims tables' User_Id column.

It should work now.

Just in case, If it does't, depending on the error message. If it says invalid column name, the all you need to do is check the column names in database and your classes. If there is a mismatch, fix it like I did with User_Id column.

If it says there is a table missing, or invalid table or table not found, then  a quick google search should land you on the table structure. Add that to your database, if any errors are popping up, fix them in the OnModelCreating method.

TIP:
You can create a default .NET project (MVC) which has Identity 2 enabled by default. Then, when you run it, your database will be auto generated, without any errors. Now that you have a working db which works with ID2, bugs and errors fixing is easier.

Popular posts from this blog

Print a receipt using a Thermal Printer with C#.NET

Automatic redirect upon session timeout using ASP.NET MVC and Javascript

Complex Master-Detail Form using Knockout.js and ASP.NET MVC