[NotMapped]
public class AppUser:IdentityUser
{
[Required]
public string RealName { get; set; } = default!;
[Required]
[ForeignKey("DepartmentId")]
public Department Department { get; set; } = default!;
}
public class Department
{
[Key]
public int DepartmentId { get; set; }
[Required]
public string Name { get; set; } = default!;
public Department? Parent { get; set; }
}
IdentityUser扩展了Department外键,在增加用户时,提示外键冲突,怎么解决?
if (ModelState.IsValid)
{
var user = new AppUser { UserName = model.UserName, RealName = model.RealName};
var dep = _departmentRepository.Departments.FirstOrDefault(d => d.DepartmentId == Id);
user.Department = dep;
if (model.Password != null)
{
var result = await userManager.CreateAsync(user, model.Password);
}
return View(model);
}
相似问题