简体   繁体   中英

Update database with passing values in entity framework database first

I have Register Model in Db the below post method in controller does the update to database . However I also want to update Login data model

[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();

        }

Below are the models created by entity framework 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; }
    }

Can you guys suggest me the call to update LoginDetail table also with the Username and Password in that post method.

thanks,

Michaeld

To update and existing LoginDetail record:

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();

To add a new LoginDetail record:

var loginDetail = new LoginDetail{
Username = details.Username,
Password = details.Password
};
DbAccess.LoginDetails.Add(loginDetail);
DbAccess.SaveChanges();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM