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?

Markdown supported.
Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)

1 Answer(s)
  • User Avatar
    0
    liangshiwei created
    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);
            }
        );
    
    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.8.0-preview. Updated on September 28, 2026, 11:44
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.