Home > OS >  How to customize the default AspNetUsers data type lengths in ASP.NET Core
How to customize the default AspNetUsers data type lengths in ASP.NET Core

Time:10-26

I know how to add and edit custom columns to AspNetUsers. I am asking how to change the default ones. I tried using the Fluent API in the DbContext file, but nothing changes. My Visual Studio is - Microsoft Visual Studio Community 2022 (64-bit) - Current Version 17.3.6, and the project is ASP.NET Core App (MVC) on .NET6

For example, I want to change the length for UserName from nvarchar(256) to nvarchar(50)

enter image description here

CodePudding user response:

For example, I want to change the length for UserName from nvarchar(256) to nvarchar(50)

You can try below:

  1. Right Click your Database and click New Query, and add below code in it:

    ALTER TABLE AspNetUsers ALTER COLUMN UserName VARCHAR (50);

2.Then click Excute.

enter image description here

2.result:

enter image description here

Update

using Entity Framework:

Refer below code(I custom user, you can just use your model) to change your DbContext:

 public class AppIdentityDbContext : IdentityDbContext<AppUser>
    {
        public AppIdentityDbContext(DbContextOptions<AppIdentityDbContext> options) : base(options) { }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder
                .Entity<AppUser>()
                .Property(x => x.UserName)
                .HasColumnType("nvarchar(50)");

            base.OnModelCreating(builder);
        }
    }

result:

enter image description here

  • Related