Upgrading ASP.NET Identity in NuGet

This morning, when I went to upgrade the Identity system in my MVC application from NuGet, I ran into issues. When I ran the application that attempted to access roles, I got an error that the model backing the dbContext had changed and that I needed to run a code first migration.

I have very little experience with code-first EntityFramework as I have always used database-first. I just find it easier to set up my database with all the foreign keys and then create my data model from that. I haven’t yet learned the benefits of code-first.

So, to continue, I found the following two resources on the web that pointed me in the right direction:

Upgrading from EntityFramework Identity 1.0 to 2.0

and

Automatic Code-First Migrations

Within those articles, I found the following package manager console commands that helped me:

Enable-Migrations –EnableAutomaticMigrations Add-Migration Nuget-Update-ASPNet-Identity

Since I had two contexts, it gave me a message that I needed to choose one or the other and even told me the exact command to use to do it. I just copied and pasted the command in the console and ran it.

Once I told it which context was my identity context, I then had to update the database. That was simple enough as all I had to do was use the following command:

Update-Database

That updated the identity database and allowed me to run my application again.

Now, whenever the identity system changes, I can easily update the data model. Though, I’m not quite sure what to do in situations where the changes are destructive (I could lose data). I need to learn more about this.