Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
How is LoginDetail related to a StudentDetail? Does LoginDetail have a StudentDetailId? Or StudentDetail have a LoginDetailId? –  von v. Apr 22 '13 at 3:28
add comment

1 Answer

up vote 0 down vote accepted

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();
share|improve this answer
    
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 2:30
    
can you post the error? –  SOfanatic Apr 22 '13 at 2:31
    
StackTrace = " at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)\r\n at System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__3[TResult](I‌​Enumerable`1 sequence)\r\n at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult... –  michaeld Apr 22 '13 at 2:37
    
(x => x.Username == details.Username); I Dont understand this part stackoverflow.com/questions/9907963/… i found this but didn't help me out answered by adrian –  michaeld Apr 22 '13 at 2: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 2:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.