Seed Roles and Users to an existing database in ASP.NET MVC 5 using Identity

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Seed Roles and Users to an existing database in ASP.NET MVC 5 using Identity

Hello forum friends,

In this tutorial, I will demonstrate seeding roles and users to an existing database in an ASP.NET MVC 5 VB.NET application using Identity framework. For simplicity, I wil use a Northwind database or you may choose another database aside from this. This database doesn't have membership tables at all.

To begin with, accomplish the steps below:
1. Create an ASP.NET MVC project (VB) and then change web.config connectionStrings element to connect to an
existing database (Northwind as my example).
2. In Package Manager Console execute command
PM> Enable-Migrations
3. In Package Manager Console execute command
PM> Add-Migration ASPMembership
* This will create a file Timespan_ASPMembership.vb inside Migrations folder with scripts to create Membership
Tables such as AspNetUsers, AspNetRoles and etc..
4. In Package Manager Console execute command
PM> Update-Database
* This will add membership tables to Northwind database.
5. To seed Roles and Users, create a class SeedRolesAndUsers.vb inside Models folder. This class references
Microsoft.AspNet.Identity and Microsoft.AspNet.Identity.EntityFramework namespaces. The Seed() method
adds Customer or Manager role if they don't exist. The remaining part of the code creates a user and assign it
to a specific role.