【发布时间】:2015-09-12 07:36:08
【问题描述】:
我是 asp.net mvc 和实体框架的新手,所以我真的需要一些帮助。
我希望将新类实现到具有一对一关系的 IdentityUser 类中。现在我有这个:
public class ApplicationUser : IdentityUser
{
public virtual MapPosition MapPosition { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<MapPosition> MapPositions { get; set; }
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
public class MapPosition
{
[Key, ForeignKey("ApplicationUser")]
public string UserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
public int PositionX { get; set; }
public int PositionY { get; set; }
}
一切都很好,新表已经创建,但我怎样才能做到这样每次创建新用户时,应用程序也会为两个属性创建默认值为 0 的 MapPosition 条目?
【问题讨论】:
-
如果
UserId也是Key对应MapPosition,为什么不将ApplicationUser中的两个属性移动? -
因为我打算开更多这样的课,我不想把所有东西都放在一个课上。
标签: asp.net-mvc entity-framework asp.net-mvc-5 asp.net-identity asp.net-identity-2