0

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

michaeld
  • 569
  • 1
  • 6
  • 10
  • How is `LoginDetail` related to a `StudentDetail`? Does `LoginDetail` have a `StudentDetailId`? Or `StudentDetail` have a `LoginDetailId`? – von v. Apr 22 '13 at 03:28

1 Answers1

0

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();
SOfanatic
  • 5,523
  • 5
  • 36
  • 57
  • Hi Sofanatic, I am not sure what the above code does so i copy pasted the above piece it throwed the error "Sequence contains no elements" for the first line. however there are some data in LoginDetails – michaeld Apr 22 '13 at 02:30
  • StackTrace = " at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)\r\n at System.Data.Objects.ELinq.ObjectQueryProvider.b__3[TResult](IEnumerable`1 sequence)\r\n at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult... – michaeld Apr 22 '13 at 02:37
  • (x => x.Username == details.Username); I Dont understand this part http://stackoverflow.com/questions/9907963/linq-query-issue-sequence-contains-no-elements i found this but didn't help me out answered by adrian – michaeld Apr 22 '13 at 02:38
  • this is assuming that a record for `LoginDetails` with a `Username` equal to `details.Username` already exists. If this is the first time it's being created then you should do the following. See answer for edit. – SOfanatic Apr 22 '13 at 02:45