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)
CodePudding user response:
For example, I want to change the length for UserName from nvarchar(256) to nvarchar(50)
You can try below:
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
.
2.result:
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: