Trả lời

6

Tôi tạo ra một lớp ApplicationUser : IdentityUser trong đó tôi ghi đè bất động sản PhoneNumber với thuộc tính [MaxLength(64)].

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; 
    } 
} 
+0

Giải pháp sạch đẹp. – Zapnologica

0

có bạn có thể thay đổi, chỉ giới hạn nvarchar nếu bạn không muốn vượt quá con số đó vì kích thước lưu trữ dữ liệu của nvarchar bằng hai lần chiều dài dữ liệu thực tế đã nhập + 2 byte. ví dụ: max hoặc 64 độ ảnh hưởng sẽ không nếu bạn nhập 5 char chuỗi

4

Bạn sẽ có thể để xác định chiều dài tùy chỉnh bằng cách sử dụng modelBuilder trong 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(); 
    } 
} 

Tôi đã thử nghiệm nó và nó hoạt động!

Để biết thêm thông tin về thao tác ánh xạ EF6 bạn có thể kiểm tra liên kết này:

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

+0

Hiện giờ có hoạt động không? xin vui lòng chấp nhận điều này như là một câu trả lời nếu nó hoạt động và bạn không có bất kỳ câu hỏi bổ sung, vv –

+0

Cảm ơn bạn đã giải pháp của bạn. Tôi tìm cách khác để làm như vậy. Tôi đã tạo lớp 'ApplicationUser: IdentityUser', trong đó tôi ghi đè thuộc tính' PhoneNumber' với thuộc tính '[MaxLength (64)]'. – sDima

Các vấn đề liên quan