I'm working on ABP framework version 7.1, with Angular UI
I renamed and set to required two identityUser columns in my dbcontext as:
builder.Entity<IdentityUser>(b =>
{
b.Property(x => x.Name).IsRequired().HasColumnName("FirstName");
b.Property(x => x.Surname).IsRequired().HasColumnName("LastName");
});
I created the migration and updated the database. It works correctly, now my database has that column names.
But when I run the data seeder, I get an error:
Cannot insert null into column name LastName, table AbpUsers, the column does not allow nulls.
So I try to create my Identity seeder as:
[Dependency(ReplaceServices = true)]
public class CustomIdentityDataSeederContributor : IdentityDataSeeder
{
public CustomIdentityDataSeederContributor(
IUnitOfWorkManager unitOfWorkManager,
IGuidGenerator guidGenerator,
IIdentityRoleRepository roleRepository,
IIdentityUserRepository userRepository,
ILookupNormalizer lookupNormalizer,
IdentityUserManager userManager,
IdentityRoleManager roleManager,
ICurrentTenant currentTenant,
IOptions<IdentityOptions> identityOptions,
IdentityUserManager identityUserManager)
: base(guidGenerator, roleRepository, userRepository, lookupNormalizer, userManager, roleManager, currentTenant, identityOptions)
{
}
public override async Task<IdentityDataSeedResult> SeedAsync(string adminEmail, string adminPassword, Guid? tenantId = null)
{
var result = new IdentityDataSeedResult();
Check.NotNullOrWhiteSpace(adminEmail, nameof(adminEmail));
Check.NotNullOrWhiteSpace(adminPassword, nameof(adminPassword));
string adminUserName = "custom";
if (adminEmail == "[email protected]")
{
return result;
}
if (tenantId != Guid.Empty && tenantId != null)
{
adminUserName = "custom2";
if (adminEmail == "[email protected]")
{
adminEmail = "[email protected]";
}
}
using (CurrentTenant.Change(tenantId))
{
//"admin" user
var adminUser = await UserRepository.FindByNormalizedUserNameAsync(
LookupNormalizer.NormalizeName(adminUserName)
);
if (adminUser != null)
{
return result;
}
adminUser = new IdentityUser(
GuidGenerator.Create(),
adminUserName,
adminEmail,
tenantId
)
{
//This should be first name and lastname
Name = adminUserName
Surname = "surname"
};
(await UserManager.CreateAsync(adminUser, adminPassword)).CheckErrors();
result.CreatedAdminUser = true;
//"admin" role
const string adminRoleName = "admin";
var adminRole = await RoleRepository.FindByNormalizedNameAsync(LookupNormalizer.NormalizeName(adminRoleName));
if (adminRole == null)
{
adminRole = new IdentityRole(
GuidGenerator.Create(),
adminRoleName,
tenantId
)
{
IsStatic = true,
IsPublic = true
};
(await RoleManager.CreateAsync(adminRole)).CheckErrors();
result.CreatedAdminRole = true;
}
(await UserManager.AddToRoleAsync(adminUser, adminRoleName)).CheckErrors();
return result;
}
}
As you can see, the column names of the IdentityUser are still the same:
{
Name = adminUserName,
Surname = "surname"
};
Instead of my new column names, so the property names inside ABP IdentityUser
class are not changed.
What do I need to do to solve that? Best regards
--Update--
I added the property LastName on MigrationService class as:
private async Task SeedDataAsync(Tenant tenant = null)
{
Logger.LogInformation($"Executing {(tenant == null ? "host" : tenant.Name + " tenant")} database seed...");
await _dataSeeder.SeedAsync(new DataSeedContext(tenant?.Id)
.WithProperty(IdentityDataSeedContributor.AdminEmailPropertyName, IdentityDataSeedContributor.AdminEmailDefaultValue)
.WithProperty(IdentityDataSeedContributor.AdminPasswordPropertyName, IdentityDataSeedContributor.AdminPasswordDefaultValue)
.WithProperty("LastName", "Test")
);
}
But still the same error!
You need to implement both DataSeed and the DataSeedContributor. Use the both classes below.
[Dependency(ReplaceServices = true)]
public class CustomIdentityDataSeedContributor : IdentityDataSeedContributor
{
private IIdentityDataSeeder _identityDataSeeder;
public CustomIdentityDataSeedContributor(IIdentityDataSeeder identityDataSeeder)
: base(identityDataSeeder)
{
_identityDataSeeder = identityDataSeeder;
}
public override Task SeedAsync(DataSeedContext context)
{
return _identityDataSeeder.SeedAsync(
"[email protected]",
"Pass1234+",
context?.TenantId
);
}
}
And
[Dependency(ReplaceServices = true)]
public class CustomIdentityDataSeeder : IdentityDataSeeder
{
public CustomIdentityDataSeeder(
IUnitOfWorkManager unitOfWorkManager,
IGuidGenerator guidGenerator,
IIdentityRoleRepository roleRepository,
IIdentityUserRepository userRepository,
ILookupNormalizer lookupNormalizer,
IdentityUserManager userManager,
IdentityRoleManager roleManager,
ICurrentTenant currentTenant,
IOptions<IdentityOptions> identityOptions,
IdentityUserManager identityUserManager)
: base(guidGenerator, roleRepository, userRepository, lookupNormalizer, userManager, roleManager, currentTenant, identityOptions)
{
}
public override async Task<IdentityDataSeedResult> SeedAsync(string adminEmail, string adminPassword, Guid? tenantId = null)
{
var result = new IdentityDataSeedResult();
Check.NotNullOrWhiteSpace(adminEmail, nameof(adminEmail));
Check.NotNullOrWhiteSpace(adminPassword, nameof(adminPassword));
string adminUserName = "custom";
if (adminEmail == "[email protected]")
{
return result;
}
if (tenantId != Guid.Empty && tenantId != null)
{
adminUserName = "custom2";
if (adminEmail == "[email protected]")
{
adminEmail = "[email protected]";
}
}
using (CurrentTenant.Change(tenantId))
{
//"admin" user
var adminUser = await UserRepository.FindByNormalizedUserNameAsync(
LookupNormalizer.NormalizeName(adminUserName)
);
if (adminUser != null)
{
return result;
}
adminUser = new IdentityUser(
GuidGenerator.Create(),
adminUserName,
adminEmail,
tenantId
)
{
//This should be first name and lastname
Name = adminUserName,
Surname = "surname"
};
(await UserManager.CreateAsync(adminUser, adminPassword)).CheckErrors();
result.CreatedAdminUser = true;
//"admin" role
const string adminRoleName = "admin";
var adminRole = await RoleRepository.FindByNormalizedNameAsync(LookupNormalizer.NormalizeName(adminRoleName));
if (adminRole == null)
{
adminRole = new IdentityRole(
GuidGenerator.Create(),
adminRoleName,
tenantId
)
{
IsStatic = true,
IsPublic = true
};
(await RoleManager.CreateAsync(adminRole)).CheckErrors();
result.CreatedAdminRole = true;
}
(await UserManager.AddToRoleAsync(adminUser, adminRoleName)).CheckErrors();
return result;
}
}
}
I have tested it and it works.