【发布时间】:2018-05-05 07:41:21
【问题描述】:
ApplicationUser 类继承 IdentityUser
public class ApplicationUser : IdentityUser
{
public DateTime BirthDate { get; set; }
public string User_type { get; set; }
public DateTime CreatedOn { get; set; }
public Enums.Sex Gender { get; set; }
public bool IsActive { get; set; }
public virtual Patient Patients { get; set; }
public int PatientID { get; set; }
}
病人分类
public class Patient
{
[Key]
public int PatientID { get; set; }
public string ProfilePicture { get; set; }
public string FName { get; set; }
public DateTime BirthDate { get; set;}
public string City { get; set; }
public string Country { get; set; }
}
控制器
public async Task<ActionResult> RegisterPatient(RegisterViewModel model)
{
MYDb db = new MYDb();
Patient pt = new Patient();
pt.BirthDate = model.BirthDate;
pt.City = model.City;
pt.Country = model.Country;
pt.ProfilePicture = model.ProfilePicture;
pt.FName = model.FName;
int Pat_id = model.PatientID;
if (ModelState.IsValid)
{
ApplicationUser myuser = new ApplicationUser();
model.CreatedOn = DateTime.Now;
model.IsActive = true;
myuser.PatientID = Pat_id;
if (ModelState.IsValid)
{
var user = model.GetUser();
db.Patients.Add(pt);
db.SaveChanges();
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
var idManager = new IdentityManager();
idManager.AddUserToRole(user.Id, model.User_type);
//await db.SaveChangesAsync();
await SignInAsync(user, isPersistent: false);
return RedirectToAction("Index", "Home");
}
else
{
AddErrors(result);
}
}
return RedirectToAction("Index", "Home");
}
// If we got this far, something failed, redisplay form
return View();
}
我想首先建立 b/w Patient 和 ApplicationUser 的关系 在患者中添加记录,然后将“Patient_ID”用作外键 ApplicationUser 表,但是当添加数据时我面临这种类型的错误
错误
注意:数据保存在 Patient 表中,但不保存在 ApplicationUser 表中,因为 ApplicationUser 没有得到“PatientID”,因此在数据库中发生了冲突。
我希望这很清楚并且不容易解决:(
【问题讨论】:
-
该错误意味着您尝试将任何值作为 PatientID 插入到 ApplicationUser 中,该值不存在于患者表的 PatientID 列中。所以你需要调试你的代码并检查代码是否正确获取了 PatientID
标签: asp.net-mvc entity-framework-6 asp.net-identity asp.net-identity-2