0

So, I have my User that inherits IdentityUser. However, I want to set the minimum length of the UserName to be 5 and the maximum length to be 30.

I tried this

public class User : IdentityUser
    {
        [Required]
        [StringLength(nameMaxLength, MinimumLength = nameMinLength)]
        public string UserName { get; set; }

        public IEnumerable<UserMovie> UsersMovies { get; set; }
    }

but it did not work. I found that I could probably set the max length via FluentAPI like this

protected override void OnModelCreating(ModelBuilder builder)
        {
            builder
                .Entity<User>()
                .Property(x => x.UserName)
                .HasMaxLength(15);

            base.OnModelCreating(builder);
        }

but nothing for the minimum length.

Progman
  • 16,827
  • 6
  • 33
  • 48
  • Does this answer your question? [Does the EF Fluent Api can setting the Minimum Length](https://stackoverflow.com/questions/34533233/does-the-ef-fluent-api-can-setting-the-minimum-length)? – Qing Guo Oct 24 '22 at 08:21

0 Answers0