Seeding ASP.NET Identity Database

In this post you will learn how to seed User’s Profile database tables (AspNetUsers and UserProfileInfoes, we created in last post) with dummy data.

Introduction

As you know ASP.NET Identity uses Entity Framework, EF can automatically create/update/drop databases when the application runs. We can specify that, this should be done every time application runs or only when the model is out of sync with the existing database or in other word it runs whenever model changes. You spent time to insert records in database and when you made any changes in model, Entity Framework deletes your database as well as records. Entity Framework recommends to use 'Database Migrations' to stop loosing Database or Records using its ‘Seed’ method (we can find it in Migration (Folder) | Configuration.cs class) which allow us to seed some dummy data in the database for testing purpose or else.

Let’s recall, what we have learned so far in ASP.NET Identity blog series.

We explored how to customize user’s profile to add new fields (FirstName, LastName, EmailID) in same database table ‘AspNetUsers’. If you missed this post, just read it before proceeding here.

We also explored how to customize user’s profile to add new fields (FirstName, LastName, EmailID) in brand new database table ‘UserProfileInfoes’. If you missed this post, just read it before proceeding here.


I recommend you to read above two posts before proceeding here, because I’m going to seed database tables (AspNetUsers and UserProfileInfoes) which needs basics understanding of the concept I have used.


Seeding AspNetUsers and UserProfileInfoes tables

There are two ways to seed database tables, let’s explore both one by one.

Older Approach

This is very first and commonly used approach, it works well. In the code given below I’m seeding a user with username ‘abhimanyu’ and password ‘abhimanyu’ (hashing the password) and also its profile information (FirstName, LastName, EmailID).

protected override void Seed(ProfileManagement.Models.ApplicationDbContext context)
{
    var hasher = new PasswordHasher();
    context.Users.AddOrUpdate(u => u.UserName,
        new ApplicationUser
        {
            UserName = "abhimanyu",
            PasswordHash = hasher.HashPassword("abhimanyu"),
            UserProfileInfo = new UserProfileInfo
            {
                FirstName = "Abhimanyu K",
                LastName = "Vatsa",
                EmailID = "[email protected]"
            }
        });

    manager.Create(user, "abhimanyu");
}

The same thing can be achieved with ASP.NET Identity specific way, here it is.

New ASP.NET Identity Approach

Whatever we did in above code can be achieved with new ASP.NET Identity approach means by using UserStore and UserManager classes.

protected override void Seed(ProfileManagement.Models.ApplicationDbContext context)
{
    var store = new UserStore<ApplicationUser>(context);
    var manager = new UserManager<ApplicationUser>(store);
    var hasher = new PasswordHasher();
    var user = new ApplicationUser
    {
        UserName = "abhimanyu",
        PasswordHash = hasher.HashPassword("abhimanyu"),
        UserProfileInfo = new UserProfileInfo
        {
            FirstName = "Abhimanyu K",
            LastName = "Vatsa",
            EmailID = "[email protected]"
        }
    };

    manager.Create(user, "abhimanyu");
}

This also works very well. We can use any approach given above.

Once you done with above code, open NuGet Package Manager Console and execute this command and you done.

Update-Database

Just run the application, and you will be able to login using ‘abhimanyu’ username and ‘abhimanyu’ password.

Hope this helps.
Thanks.

Comments

  1. thanks really its a very good book

    ReplyDelete
  2. Neglect the comment about picture problem, the problem was on my side... with blogspot.com..

    ReplyDelete
  3. Thanks lot, it works to me. You save my time..cheers

    ReplyDelete
  4. Thanks lot, you save my time. It works very well..!

    ReplyDelete
  5. how to view all user and password list from AspNetUsers table?

    ReplyDelete

Post a Comment

Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System