Open Closed

Adding unique integer extra property to IdentityUser #5212


User avatar
0
dkaczor created
  • ABP Framework version: v7.0.1
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no
  • Exception message and stack trace:
  • Steps to reproduce the issue:"

I want to add an extra property to IdentityUser. It's supposed to contain unique integer values, like a second Id for user. I'd like it to be generated automatically when adding row and not being editable. So I used given configuration in MyAppEfCoreEntityExtensionMappings:

            ObjectExtensionManager.Instance
                     .MapEfCoreProperty<IdentityUser, int>(
                         "Xyz",
                         (entityBuilder, propertyBuilder) =>
                         {
                             entityBuilder.HasIndex("Xyz").IsUnique();
                             propertyBuilder.ValueGeneratedOnAdd();
                         }
                     );

Problem is that now when I add new user through UI, I get an error:

2023-06-07 17:03:36.458 +02:00 [ERR] An error occurred while saving the entity changes. See the inner exception for details. Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details. ---> Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot update identity column 'Xyz'.

How do I configure it so it works to my requirements?


1 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can try this:

    ObjectExtensionManager.Instance
        .MapEfCoreProperty<IdentityUser, int>(
            "Xyz",
            (entityBuilder, propertyBuilder) =>
            {
                entityBuilder.HasIndex("Xyz").IsUnique();
        
                propertyBuilder.ValueGeneratedOnAdd();
                propertyBuilder.Metadata.SetAfterSaveBehavior(PropertySaveBehavior.Ignore);
            }
        );
    
Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11