0

我在 Db 中有注册模型,控制器中的以下 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首先创建的模型

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; }
    }

你们能否建议我在该帖子方法中使用用户名和密码来更新 LoginDetail 表。

谢谢,

迈克尔德

4

1 回答 1

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();
于 2013-04-22T02:22:29.100 回答