身份用户的外键
Foreign key on Identity User
我为默认身份应用程序用户添加了一个外键 class。
问题是:当我创建一个新用户时,数据库上的 ForeignKey 参数仍然为 NULL。
我的应用程序用户class。
public class ApplicationUser : IdentityUser
{
//Foreing Key
public Guid SalaId;
public virtual Sala Sala { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
}
}
上下文
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext() : base("someConnectionString", throwIfV1Schema: false)
{
}
public DbSet<Sala> Salas { get; set; }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
在我的 AccountController 中,我尝试添加一个新用户
private ApplicationDbContext db = new ApplicationDbContext();
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
//here I find the 'Sala' with the 'numero' parameter from View
var x = db.Salas. Where(y => y.Numero == model.Sala).FirstOrDefault();
var user = new ApplicationUser { UserName = model.Username,
Email = model.Email, SalaId = x.Id };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
}
使用 属性 而不是 SalaId
的字段
public class ApplicationUser : IdentityUser
{
//Foreing Key
public Guid SalaId { get; set; }
}
我为默认身份应用程序用户添加了一个外键 class。 问题是:当我创建一个新用户时,数据库上的 ForeignKey 参数仍然为 NULL。
我的应用程序用户class。
public class ApplicationUser : IdentityUser
{
//Foreing Key
public Guid SalaId;
public virtual Sala Sala { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
}
}
上下文
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext() : base("someConnectionString", throwIfV1Schema: false)
{
}
public DbSet<Sala> Salas { get; set; }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
在我的 AccountController 中,我尝试添加一个新用户
private ApplicationDbContext db = new ApplicationDbContext();
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
//here I find the 'Sala' with the 'numero' parameter from View
var x = db.Salas. Where(y => y.Numero == model.Sala).FirstOrDefault();
var user = new ApplicationUser { UserName = model.Username,
Email = model.Email, SalaId = x.Id };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
}
使用 属性 而不是 SalaId
的字段 public class ApplicationUser : IdentityUser
{
//Foreing Key
public Guid SalaId { get; set; }
}