还有一个 userstats 表,我想要一对一的关系,但是当我尝试注册用户时出现上述错误。感谢您的帮助!
用户表:
public class User
{
[Key]
public int ID { get; set; }
public int UserStatsID { get; set; }
public virtual UserStats UserStats { get; set; }
}
用户统计表
[Key]
public int ID { get; set; }
public int UserID { get; set;}
[Required]
public User User { get; set; }
注册详细信息控制器
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(User user)
{
if (ModelState.IsValid)
{
db.Users.Add(user);
db.SaveChanges();
if (user.Gender == Gender.Male )
{
return RedirectToAction("RegisterMale", "RegisterStats", new {id = user.ID });
}
else
{
return RedirectToAction("RegisterFemale", "RegisterStats", new { id = user.ID });
}
}
return View(user);
}