﻿Open visual studio and create new project with individual identity.
open web.config and copy paste connection string.
  <connectionStrings>
    <!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-PhotoApp-20250923024404.mdf;Initial Catalog=aspnet-PhotoApp-20250923024404;Integrated Security=True"
      providerName="System.Data.SqlClient" />-->
    <add name="DefaultConnection" connectionString="data source=SCIL-GWL-235;initial catalog=PhotoApp;persist security info=True;user id=bhupi1;password=Welcome01??;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient"/>
  </connectionStrings>

Now open package manager console and type
Enable-migrations
update-database
it will create the database with the default tables.
Now you can proceed with your further development.


TO RENAME THE ASPNETUSERS TABLE:::
>Open the identityModels.cs file and add the below lines
 protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // Rename Identity tables
            modelBuilder.Entity<ApplicationUser>().ToTable("Registration");
            modelBuilder.Entity<IdentityRole>().ToTable("Roles");
            modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles");
            modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims");
            modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins");
        }
>in package manger > Add-Migration RenameAspNetUsersToRegistration  > update-database
Thats it... Done

Below is a clean and complete way to add Roles, and assign a Role to a User during registration in ASP.NET MVC + EF6 + Identity 2.0, with your renamed tables (Registration, Roles, etc.) already considered.

✅ STEP 1 — Enable RoleManager in IdentityConfig

Open App_Start/IdentityConfig.cs and add this:

public class ApplicationRoleManager : RoleManager<IdentityRole>
{
    public ApplicationRoleManager(IRoleStore<IdentityRole, string> store)
        : base(store) { }

    public static ApplicationRoleManager Create(
        IdentityFactoryOptions<ApplicationRoleManager> options,
        IOwinContext context)
    {
        var roleStore = new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>());
        return new ApplicationRoleManager(roleStore);
    }
}


And register it in Startup.Auth.cs:

app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);

✅ STEP 2 — Seed Roles (Admin, User, etc.)

Open Migrations/Configuration.cs

Add this in Seed():

protected override void Seed(ApplicationDbContext context)
{
    var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));

    string[] roles = { "Admin", "User", "Manager" };

    foreach (var r in roles)
    {
        if (!roleManager.RoleExists(r))
        {
            roleManager.Create(new IdentityRole { Name = r });
        }
    }
}


Run:

update-database


This creates:

Admin

User

Manager

in Roles table.

✅ STEP 3 — Add Role Dropdown in Registration Page
In RegisterViewModel
public class RegisterViewModel
{
    [Required]
    public string Email { get; set; }

    [Required]
    public string Password { get; set; }

    public string RoleName { get; set; }  // 👈 Add this
}

In Register.cshtml

Add dropdown:

<div class="form-group">
    @Html.LabelFor(m => m.RoleName, "Select Role")
    @Html.DropDownListFor(m => m.RoleName,
        new SelectList(ViewBag.Roles, "Value", "Text"),
        "Select Role",
        new { @class = "form-control" })
</div>

✅ STEP 4 — Load Roles from DB in GET Register

In AccountController.cs:

public ActionResult Register()
{
    var roles = RoleManager.Roles.Select(r => new { Value = r.Name, Text = r.Name }).ToList();
    ViewBag.Roles = roles;

    return View();
}

✅ STEP 5 — Assign Role to User After Registration

In AccountController → Register (POST):

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = new ApplicationUser { UserName = model.Email, Email = model.Email };

        var result = await UserManager.CreateAsync(user, model.Password);

        if (result.Succeeded)
        {
            if (!string.IsNullOrEmpty(model.RoleName))
            {
                await UserManager.AddToRoleAsync(user.Id, model.RoleName);   // 👈 Assign role
            }

            return RedirectToAction("Index", "Home");
        }

        AddErrors(result);
    }

    // reload roles again if model invalid
    ViewBag.Roles = RoleManager.Roles
        .Select(r => new { Value = r.Name, Text = r.Name })
        .ToList();

    return View(model);
}


> Role Add dropdown ke baad error ayega, to account controller mai jaise user ke liye add hai waise role ka add krna, code isi file ka copy kr lena ismei fixed hai.
private ApplicationUserManager _userManager;        private ApplicationRoleManager _roleManager;  ........... isse related

