Search code examples
ef-code-firstaspnetboilerplate

The override UserName string length did not work when calling UserManager.CreateUserAsync, even the db has the correct string length.


The issue is quite simple, I modified the default length(32) for username property to 500, but it seems that the code is stuck to 32. Any idea why this happens?

The UserName string length for AbpUsers:

public class User : AbpUser<User>
{
    [MaxLength(500)]
    public override string UserName { get; set; }
}

And this has successfully updated the db UserName length.

But when I called

CheckErrors(await UserManager.CreateAsync(user));

It would return :

System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. 

What troubles me is that this kind of exception can not be captured by SaveChanges:

public override int SaveChanges()
    {
        try
        {
            return base.SaveChanges();
        }
        catch (DbEntityValidationException e)
        {
            foreach (var eve in e.EntityValidationErrors)
            {
                Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                    eve.Entry.Entity.GetType().Name, eve.Entry.State);
                foreach (var ve in eve.ValidationErrors)
                {
                    Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                        ve.PropertyName, ve.ErrorMessage);
                }
            }
            throw;
        }
   }

Your help would be much appreciated!


Solution

  • After hours of comparison with abp template, I found out the reason why this happens is because I used [MaxLength] instead of [StringLength] for User Entity.