我在Db中有Register Model,控制器中的post post方法会对数据库进行更新。但是我也想更新登录数据模型
[HttpPost]
public ActionResult Register(StudentDetail details)
{
if (DbAccess.LoginDetails.FirstOrDefault(student => student.Username == details.Username) == null)
{
DbAccess.StudentDetails.Add(details);
**//here i also want to update login table with added details in database**
DbAccess.SaveChanges();
return RedirectToAction("HomePage");
}
return View();
}
以下是实体框架db first
创建的模型public StudentDetail()
{
this.UserFriends = new HashSet<UserFriend>();
}
public string StudentName { get; set; }
public string UnivName { get; set; }
public string City { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string EmailId { get; set; }
public virtual ICollection<UserFriend> UserFriends { get; set; }
}
public partial class LoginDetail
{
public string Username { get; set; }
public string Password { get; set; }
}
你们可以建议我使用该post方法中的用户名和密码更新LoginDetail表。
感谢,
迈克尔答案 0 :(得分:0)
更新现有的LoginDetail
记录:
var loginDetail = DbAccess.LoginDetails.Single(x => x.Username == details.Username);
loginDetail.Username = details.Username;
loginDetail.Password = details.Password;
DbAccess.Entry(loginDetail).State = EntityState.Modified;
DbAccess.SaveChanges();
添加新的LoginDetail
记录:
var loginDetail = new LoginDetail{
Username = details.Username,
Password = details.Password
};
DbAccess.LoginDetails.Add(loginDetail);
DbAccess.SaveChanges();