I am trying to insert a value from the standard MVC registration form (namely the email field), this will be the primary key and FK in another table. My student table only contains email, I want to insert the same value(email field) that is submitted at registration to the aspnetusers table. I have written this code so far:
var student = new Student { email = model.Email };
assigning the value and then
db.Students.Add(student);
db.SaveChanges();
to perform the action, I assume this is done in the Registration controller. Please advise if I have placed it in the wrong section. I am currently getting a
Please see code below:
AccountController.cs
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var student = new Student { email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
string callbackUrl = await SendEmailConfirmationTokenAsync(user.Id, "Confirm your account");
ViewBag.Message = "Check your email and confirm your account, you must be confirmed "
+ "before you can log in.";
db.Students.Add(student);
db.SaveChanges();
return View("Info");
//return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
Student.cs
public class Student
{
[Key, ForeignKey("User")]
public string email { get; set; }
public virtual ApplicationUser User { get; set; }
}