sDi*_*ima 5 asp.net-mvc optimization entity-framework asp.net-identity
我在我的MVC 5项目中使用Asp.Net Identity 2.0.
为什么列PhoneNumber使用[nvarchar](max)在SQL Server数据库中的表[dbo].[AspNetUsers]?
我可以将此更改为[nvarchar](64),例如?
我创建了一个类ApplicationUser : IdentityUser,我PhoneNumber用属性覆盖属性[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;
}
}
Run Code Online (Sandbox Code Playgroud)
您应该能够在ApplicationDbContext中使用modelBuilder指定自定义长度
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();
}
}
Run Code Online (Sandbox Code Playgroud)
我已经对其进行了测试,并且可以正常工作!
有关操作EF6映射的更多信息,您可以查看以下链接:
http://msdn.microsoft.com/zh-CN/data/jj819164#classes
| 归档时间: |
|
| 查看次数: |
1319 次 |
| 最近记录: |