How to seed an AppUser in abp framework

Viewed 1312

I'm trying to seed and AppUser with Entity framework database in using the abp.framework (version 3). I know how to seed a IdentityUser using IdentityUserManager but can not find any documentation of how to seed an entity that extends abpUser like appUser. I'm setting the password when I'm seeding (for demo data purposes).

1 Answers

I was able to add users on seed using the following.

This code adds 2 admin users and 1 user with no roles. Add this class to your Domain project in a separate file and when you run the DbMigrator project the users will be created. Note this is a simplified version of what I did and I've cut it down to answer the question, I've not tried it in a live project but everything you need is in there.

using System;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Identity;

namespace Your.Project
{
    class AddUsersDataSeederContributor
        : IDataSeedContributor, ITransientDependency
    {
        private readonly IdentityUserManager _identityUserManager;

        public AddUsersDataSeederContributor(IdentityUserManager identityUserManager)
        {
            _identityUserManager = identityUserManager;
        }

        public async Task SeedAsync(DataSeedContext context)
        {
            // Add users
            IdentityUser identityUser1 = new IdentityUser(Guid.NewGuid(), "test_user1", "testuser1@email.com");
            await _identityUserManager.CreateAsync(identityUser1, "1q2w3E*");
            await _identityUserManager.AddToRoleAsync(identityUser1, "Admin");

            IdentityUser identityUser2 = new IdentityUser(Guid.NewGuid(), "test_user2", "testuser2@email.com");
            await _identityUserManager.CreateAsync(identityUser2, "1q2w3E*");
            await _identityUserManager.AddToRoleAsync(identityUser2, "Admin");

            IdentityUser identityUser3 = new IdentityUser(Guid.NewGuid(), "test_user3", "testuser3@email.com");
            await _identityUserManager.CreateAsync(identityUser3, "1q2w3E*");
            //await _identityUserManager.AddToRoleAsync(identityUser3, "Admin");  // Intentionally not making this user and admin
        }
    }
}
Related