Base solution for your next web application
Open Closed

Increase UserName Length #5222


User avatar
0
PhilWynn created

Hi,

I successfully managed to increase the UserName field length based on advice from a previous post, as follows:

public class User : AbpUser<User>
{
       ...

        [StringLength(256)]
        public override string UserName { get; set; }

        ...
}

However, since upgrading to ABP v3.5, I am unable to migrate my database. This seems to be due to the fact that UserAccount.UserLength now has a maximum length of 32 and the migration would cause its contents to truncate.

How can I increase the MaxLength of UserAccount.UserName ? I have tried creating a custom UserAccount class and overriding UserName with the required MaxLength, but this does not work.


11 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You can check <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/issues/3460#issuecomment-394612406">https://github.com/aspnetboilerplate/as ... -394612406</a>.

  • User Avatar
    0
    PhilWynn created

    Hi,

    That got me some of the way there...

    I added the following to dBContext..

    modelBuilder.Entity<User>().Property(u => u.UserName).HasMaxLength(256);
    modelBuilder.Entity<UserAccount>().Property(u => u.UserName).HasMaxLength(256);
    

    I then performed a migration. This succeeded, and User.UserName and UserAccount.UserName are now set at nvarchar(256)

    However, attempting to create a user, I get an Internal Server Error, with the following message in the log file:

    ERROR 2018-06-07 15:11:48,617 [10 ] loud.EntityFramework.IppexCloudDbContext - There are some validation errors while saving changes in EntityFramework: ERROR 2018-06-07 15:11:48,617 [10 ] loud.EntityFramework.IppexCloudDbContext - - UserName: The field UserName must be a string with a maximum length of 32.

    So, something is still holding on to the 32 character limit.

    I am using AspNetZero v4 MVC with Abp v3.5

  • User Avatar
    0
    ismcagdas created
    Support Team

    @PhilWynn sorry, I forgot to mention. You also need to change the related DTO classes. They probably have MaxLenght attribute on Username fields.

  • User Avatar
    0
    PhilWynn created

    Hi,

    I already did this. I have had the long UserName working for some time now on a previous version of ABP.

    The error I am getting is since updating to v3.5 ABP and comes from Entity Framework (see log text in previous post)

    Cheers

  • User Avatar
    0
    PhilWynn created

    Any more thoughts on this please...

  • User Avatar
    0
    alper created
    Support Team

    hi,

    to increase username length you set the AbpUserBase.MaxUserNameLength before base.OnModelCreating(modelBuilder); If you do this you don't need to change DTOs. remove all migrations before you do this

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
            AbpUserBase.MaxUserNameLength = 999;
            base.OnModelCreating(modelBuilder); 
            //...
     
    }
    
  • User Avatar
    0
    PhilWynn created

    Hi,

    AbpUserBase.MaxUserNameLength is a constant. I cannot change this...

  • User Avatar
    0
    alper created
    Support Team

    Yeah you are right. I wish they were static not const. So you are still getting the Entity Framework exception ( UserName: The field UserName must be a string with a maximum length of 32.) ?

    You might have forgotten to change the related CSHTML files that's used for validation but this will not cause the Entity Framework exception. Check your database table whether the migration is successfully applied or not.

  • User Avatar
    0
    alper created
    Support Team

    One more thing;

    You also need to increase the length of 'NormalizedUserName' field of User entity and 'UserName' field of UserAccount entity.

    See <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/issues/3460">https://github.com/aspnetboilerplate/as ... ssues/3460</a>

  • User Avatar
    0
    PhilWynn created

    Hi,

    Yes, I can confirm that the database has been migrated successfully.

    NormalizedUserName does not exist in the MVC version.

    Looks like an issue has now been created to increase the max length of UserName:

    <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/issues/3502">https://github.com/aspnetboilerplate/as ... ssues/3502</a>

  • User Avatar
    0
    alper created
    Support Team

    ok. so let's track the issue.