Why in Asp.Net Identity 2.0 PhoneNumber [nvarchar] (max)

I am using Asp.Net Identity 2.0 in my MVC 5 project.

Why is the PhoneNumber column used by [nvarchar](max) in the SQL Server database table [dbo].[AspNetUsers] ?

Is it possible to change this to [nvarchar](64) , for example?

+5
source share
3 answers

I created the ApplicationUser : IdentityUser class in which I override the PhoneNumber property with the [MaxLength(64)] attribute.

 public class ApplicationUser : IdentityUser { [MaxLength(64)] public override string PhoneNumber { get; set; } public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) { var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); return userIdentity; } } 
+6
source

You can specify a custom length using modelBuilder in ApplicationDbContext

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) { } static ApplicationDbContext() { // Set the database intializer which is run once during application start // This seeds the database with admin user credentials and admin role Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer()); } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Properties<string>() .Where(x => x.Name == "PhoneNumber") .Configure(c => c.HasMaxLength(64)); } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } } 

I tested it and it works!

For more information on managing EF6 mappings, you can check this link:

http://msdn.microsoft.com/en-us/data/jj819164#classes

+4
source

yes, you can change, just limit nvarchar if you do not want to exceed this number, because the size of the nvarchar storage is twice the actual length of the entered data + 2 bytes. ex: max or 64 wont effect size if you enter 5 char string

0
source

Source: https://habr.com/ru/post/1202969/


All Articles