2

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

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.