skip to Main Content

In abp framwork how can I add a new field to identityUserRole?

I am facing the following error to add a new field to the table IdentityUserRole I will be glad if someone can guide me.

The type 'Volo.Abp.Identity.IdentityUserRole' 
must be convertible to 'Volo.Abp.Data.IHasExtraProperties' 
in order to use it as parameter 'TEntity' in the generic method 'ObjectExtensionManager Volo.Abp.ObjectExtending.EfCoreObjectExtensionManagerExtensions.MapEfCoreProperty<TEntity,TProperty>(this ObjectExtensionManager, string, Action<EntityTypeBuilder,PropertyBuilder>)'

2

Answers


  1. This is how you can add a new field to UserRole with code logic:

    {
        public string YourNewField { get; set; }
    }
    

    In OnModelCreating in DbContext:

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
    
        builder.Entity<AppUserRole>(b =>
        {
            b.Property(e => e.YourNewField).HasMaxLength(128); 
        });
    }
    

    You can then create a migration and update the migration.

    Login or Signup to reply.
  2. Open ModuleExtensionConfigurator class inside the Domain.Shared project of your solution and change the ConfigureExtraProperties method like below:

    ObjectExtensionManager.Instance.Modules()
            .ConfigureIdentity(identity =>
            {      
                identity.ConfigureRole(role =>
                    role.AddOrUpdateProperty<string>("YourFieldName", property =>
                    {
                        property.Attributes.Add(new RequiredAttribute()); // if it's Required
                        property.Attributes.Add(new StringLengthAttribute(64) { MinimumLength = 4 }); // Specify Your string length if your field type is string
                    }));
            });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search