0
我很新的MVC5和.NET。我使用的ASP應用程序用戶,並有一個額外的表病人:從另一個表生成表 - MVC5
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid PatientId { get; set; }
[ForeignKey("User")]
[Display(Name = "User Id")]
public string UserId { get; set; }
public virtual ApplicationUser User { get; set; }
我的AccountController是:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser
{
UserName = model.Email,
Email = model.Email,
Title = model.Title,
FirstName = model.FirstName,
LastName = model.LastName,
DOB = model.DOB,
Gender = model.Gender,
ContactNumber = model.ContactNumber,
Address1 = model.Address1,
Address2 = model.Address2,
Address3 = model.Address3,
County = model.County,
Postcode = model.Postcode
};
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
//Create patient ID
Patient patient = new Patient();
patient.PatientId = Guid.NewGuid();
patient.UserId = user.Id;
db.Patients.Add(patient);
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
return View(model);
}
在註冊時我想創建患者的一個實例,這將產生連同patientId引用外鍵用戶標識。但是,當我在註冊時註冊新用戶時,不會創建相應的患者。 任何意見將不勝感激。
感謝
這個作品謝謝你! – coto2