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).
How to seed an AppUser in abp framework
Asked Answered
github.com/abpframework/abp/discussions/6909 –
Formation
github.com/abpframework/abp/issues/6938 –
Shade
The above links do not provide any insight –
Bolshevism
Did you check docs.abp.io/en/abp/latest/Data-Seeding? You can also write your own dataseed contributor like github.com/abpframework/abp/blob/dev/modules/identity/src/… –
Adoree
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. –
Bolshevism
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", "[email protected]");
await _identityUserManager.CreateAsync(identityUser1, "1q2w3E*");
await _identityUserManager.AddToRoleAsync(identityUser1, "Admin");
IdentityUser identityUser2 = new IdentityUser(Guid.NewGuid(), "test_user2", "[email protected]");
await _identityUserManager.CreateAsync(identityUser2, "1q2w3E*");
await _identityUserManager.AddToRoleAsync(identityUser2, "Admin");
IdentityUser identityUser3 = new IdentityUser(Guid.NewGuid(), "test_user3", "[email protected]");
await _identityUserManager.CreateAsync(identityUser3, "1q2w3E*");
//await _identityUserManager.AddToRoleAsync(identityUser3, "Admin"); // Intentionally not making this user and admin
}
}
}
© 2022 - 2024 — McMap. All rights reserved.