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).
Asked
Active
Viewed 2,192 times
6
-
https://github.com/abpframework/abp/discussions/6909 – jazb Jan 03 '21 at 23:40
-
https://github.com/abpframework/abp/issues/6938 – berkansasmaz Jan 04 '21 at 08:25
-
The above links do not provide any insight – Ryan Layton Jan 08 '21 at 09:18
-
Did you check https://docs.abp.io/en/abp/latest/Data-Seeding? You can also write your own dataseed contributor like https://github.com/abpframework/abp/blob/dev/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityDataSeedContributor.cs – gterdem Jan 11 '21 at 16:15
-
Thank you, to clarify, my question is how to seed an AppUser within dataseed contributor. I know how to seed data just not how to seed an AppUser. – Ryan Layton Jan 14 '21 at 22:11
1 Answers
4
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
}
}
}

Dean
- 121
- 1
- 2