4

I created a new required foreign key column for the AbpUsers table; the problem started when I ran the data seeder. it is throwing the following error:

Cannot insert the value NULL into column 'TimeZoneId', table 'dbo.AbpUsers'; column does not allow nulls. INSERT fails.

That happened because it was trying to insert a null TimeZoneId. After all, it does not know where to get it... The ABP framework manages the user seeder. How can I tell the identity seeder to get that TimeZoneId from the TimeZone table if the identity seeder is managed by the framework?

Extension configurator:

ObjectExtensionManager.Instance.Modules().ConfigureIdentity(identity =>
{
    identity.ConfigureUser(user =>
    {
        user.AddOrUpdateProperty<Guid>(
            "TimeZoneId", 
            property => { 
                property.UI.Lookup.Url = "/api/app/time-zone";
                property.UI.Lookup.DisplayPropertyName = "description";
                property.DisplayName = L("::TimeZone"); 
            }
        );
    });
});

Extension mappings:

ObjectExtensionManager.Instance.MapEfCoreProperty<IdentityUser, Guid?>(
    AggregatorConsts.TimeZonePropertyName, (_, propertyBuilder) =>
    {
        propertyBuilder.IsRequired();
    }
});

DbContext:

builder.Entity<IdentityUser>(b =>
{
    b.HasOne(typeof(TimeZone))
        .WithMany()
        .HasForeignKey("TimeZoneId")
        .IsRequired();
});

Note, I'm using OpenIddict auth server.

Florian
  • 1,019
  • 6
  • 22
Jesus
  • 331
  • 1
  • 4
  • 19

1 Answers1

0

You need to implement your own identity seeder. Here is a similar question to the identity seeding. Use the current identity implementation of your used version.

ABP identity versions -> https://docs.abp.io/en/abp/latest/Modules/Identity

Implementation suggestion -> https://support.abp.io/QA/Questions/894/How-to-prevent-adminabpio-admin-user-seeding

Do not forget to add the replace attribute

[Dependency(ReplaceServices = true)]
public class CustomIdentityDataSeeder : IdentityDataSeeder
{

}
Manuel1234
  • 190
  • 8
  • It makes sense, but how can I call the TimeZoneId field in the SeedAsync method if it does not exist on IdentityUser entity? – Jesus May 11 '23 at 17:59