Open Closed

Extending the IdentityUser Object with Enum property #994


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

Hi,

I am extending the IdentityUser Object to add some custom properties and have successfully added a few following the advice here:

https://docs.abp.io/en/abp/latest/Module-Entity-Extensions https://community.abp.io/articles/how-to-add-custom-property-to-the-user-entity-6ggxiddr

However I hit a problem If I want to add a property of type Enum, for example :

 user.AddOrUpdateProperty<Title>(
                      UserConsts.TitleName,
                      options =>
                      {
                          options.Attributes.Add(new RequiredAttribute());
                          options.UI.OnTable.IsVisible = false; 
                      }
                    );

where 'Title' is the Enum :

 public enum Title
    {
        Mr = 0,
        Mrs = 1,
        Ms = 2,
        Dr = 2
    }

When creating the UI to manage the user I get the following error...

    An unhandled exception occurred while processing the request.
Exception: No items provided for select attribute.
Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form.AbpSelectTagHelperService.GetSelectItems(TagHelperContext context, TagHelperOutput output)

How do I instruct the UI to get the select elements from the values of the Enum?

Many Thanks


10 Answer(s)
  • User Avatar
    0
    nick.coleman created

    Did I ask this question incorrectly?

    If there is no answer to this can you close so I can claim my question back please

  • User Avatar
    0
    gterdem created
    Senior .NET Developer

    Hello nick.coleman, sorry for late response.

    It seems you have misconfiguration here:

    user.AddOrUpdateProperty<Title>( UserConsts.TitleName, options => { options.Attributes.Add(new RequiredAttribute()); options.UI.OnTable.IsVisible = false; } );

    Type should be enum instead of Title

    user.AddOrUpdateProperty<enum>( UserConsts.TitleName, options => { options.Attributes.Add(new RequiredAttribute()); options.UI.OnTable.IsVisible = false; } );

    Which should result like this:

  • User Avatar
    0
    nick.coleman created

    Hi,

    Thanks but this doesn't compile for me?.

    Invalid expression term 'enum'

    Thanks

    Nick

  • User Avatar
    0
    gterdem created
    Senior .NET Developer

    Hello,

    I am sharing the configuration I tried to reproduce below:

    Title.cs file under Domain.Shared:

    public enum Title
    {
        Mr = 0,
        Mrs = 1,
        Ms = 2,
        Dr = 3
    }
    

    MyProjectNameModuleExtensionConfiguratior.cs under Domain.Shared: <br>

    public static class BookStoreModuleExtensionConfigurator
    {
        private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner();
    
        public static void Configure()
        {
            OneTimeRunner.Run(() =>        {
                ConfigureExistingProperties();
                ConfigureExtraProperties();
            });
        }
    
        private static void ConfigureExistingProperties()
        {
        }
    
        private static void ConfigureExtraProperties()
        {
            ObjectExtensionManager.Instance.Modules()
                .ConfigureIdentity(identity =>            {
                    identity.ConfigureUser(user =>                {
                        user.AddOrUpdateProperty<Title>(
                            "Title",
                            options =>                        {
                                options.Attributes.Add(new RequiredAttribute());
                                options.UI.OnTable.IsVisible = false;
                            }
                        );
                    });
                });
        }
    }
    

    <br> Your configuration is correct, my apologies. However i failed to reproduce the problem. I created two different usersvia New User modal under Administration->Identity Management->Users page. <br>

    When creating the UI to manage the user I get the following error...

    Can you explain more about this? Are you replacing Identity Management -> Users list/create/edit pages? If that is the case, you can check how extended properties are shown in User Create Modal.

  • User Avatar
    0
    nick.coleman created

    Hi,

    Thanks for the quick response... Im not replacing the UI.

    I think it maybe due to the way i have the value configured in the DB? The modal works when the title is null in the DB but as soon as i save a user with a value for 'Title' field i can no longer open the modal for that user and i receive the error above.

    Here is the EF configuration ; ObjectExtensionManager.Instance .MapEfCoreProperty<IdentityUser, string>( UserConsts.TitleName, (entityBuilder, propertyBuilder) => { propertyBuilder.HasMaxLength(UserConsts.TitleLength); } );

    Does this look ok?

    I can actually create users fine, I have managed to create users but as soon as i try and edit them i get the error... are you able to try this in your example?

    Thanks

    nick

  • User Avatar
    0
    gterdem created
    Senior .NET Developer

    It seems there is a problem with your EfCore configuration;

    Here is my configuration under EntityFrameworkCore project: <br>

    public static class BookStoreEfCoreEntityExtensionMappings
    {
        private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner();
    
        public static void Configure()
        {
            BookStoreGlobalFeatureConfigurator.Configure();
            BookStoreModuleExtensionConfigurator.Configure();
    
            OneTimeRunner.Run(() =>        {
                ObjectExtensionManager.Instance
                    .MapEfCoreProperty<IdentityUser, Title>(
                        "Title",
                        (entityBuilder, propertyBuilder) =>                    {
                            propertyBuilder.HasConversion<string>(); // If you want your Title to be seen as string
                        }
                    );
            });
        }
    }
    

    This is the created migration under EntityFrameworkCore.DbMigrations project: <br>

    public partial class Added_EfCore_Mapping : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<string>(
                name: "Title",
                table: "AbpUsers",
                type: "nvarchar(max)",
                nullable: false,
                defaultValue: "");
        }
    
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "Title",
                table: "AbpUsers");
        }
    }
    

    This is AbpUsers table after adding then updating a user:

  • User Avatar
    0
    Mohammad created

    Is the migration code generated automatically from the Efcore configuration or we need to manually write the migration code?

    I have been following the above approach but the add-migration creates and empty migration file.

  • User Avatar
    0
    gterdem created
    Senior .NET Developer

    You need to add migration manually. Your new migration should have been generated automatically afterwards. You can check documentation for more.

    I have been following the above approach but the add-migration creates and empty migration file.

    That is the problably your EfCore configuration is not correct. Check the Configure method of MyProjectEfCoreEntityExtensionMappings under EntityFrameworkCore project.

  • User Avatar
    0
    nick.coleman created

    @gterdem

    Thanks you for your help!

    This is now working....

    This was the key part

    OneTimeRunner.Run(() => { ObjectExtensionManager.Instance .MapEfCoreProperty<IdentityUser, Title>( "Title", (entityBuilder, propertyBuilder) => { propertyBuilder.HasConversion<string>(); // If you want your Title to be seen as string } ); });

  • User Avatar
    0
    Mohammad created

    How can we use the extended property in LINQ to query the database?

Made with ❤️ on ABP v9.1.0-preview. Updated on December 26, 2024, 06:07