none
update multiple table relation in code RRS feed

  • Question


  • i need to update relation many to many between three tables

    employee and courses and employeecourses

    when update course id in table employeecourse not done

    my model customemployee2

    namespace WebCourse.Models {
      public class Customemployee2
       {
          public string Name { get; set; }
            public int Salary { get; set; }
            public string Email { get; set; }
         public int  DistrictId { get; set; }
    
          public int CourseId { get; set; }
          public IEnumerable<SelectListItem> Districts { set; get; }
    
            public List<EmployeeCourse> Courses { get; set; }
            public List<EmployeeLangage> Langs { get; set; }
        } }



     [HttpPost]
            public ActionResult Edit(int id,Customemployee2 cust2)
            {
                Employee old = db.Employees.Find(id);
                if (old != null)
                {
                    old.Name = cust2.Name;
                    old.Email = cust2.Email;
                    old.DistrictId = cust2.DistrictId;
                    old.Salary = cust2.Salary;
                   //what i write here to update courseId in employeecourse where select course id
    
                    db.SaveChanges();
                    return RedirectToAction("Index");
                    db.Employees.Include
    
    
                }
                return View(cust2);
            }


    how to update courseid in table employeecourse
    Friday, August 26, 2016 2:10 AM

All replies

  • can you help me if possible
    Friday, August 26, 2016 2:41 AM
  • Hi eng-ahmed-salah,

    According to your code snippet, it seems that you use a one-to-many relationship. you could remove old Courses and add new Courses, like this:

    [HttpPost]
            public ActionResult Edit(int id,Customemployee2 cust2)
            {
                Employee old = db.Employees.Find(id);
                if (old != null)
                {
                    old.Name = cust2.Name;
                    old.Email = cust2.Email;
                    old.DistrictId = cust2.DistrictId;
                    old.Salary = cust2.Salary;
                   //what i write here to update courseId in employeecourse where select course id
                    old.Courses.Clear();
                 List<EmployeeCourse> courses = new List<EmployeeCourse>();
    
                foreach(var item in coursesIds)
                {
                    EmployeeCourse ecour = new EmployeeCourse();
                    ecour.EmployeeId = emp.Id;
                    ecour.CourseId = Convert.ToInt32(item);
                    courses.Add(ecour);
                }
                  old.Courses = courses 
                    db.SaveChanges();
                    return RedirectToAction("Index");
                    db.Employees.Include
                }
                return View(cust2);
            }

    If you want to use many-to-many relationship, please refer to:

    http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx

    http://www.entityframeworktutorial.net/EntityFramework4.3/update-many-to-many-entity-using-dbcontext.aspx

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, August 26, 2016 4:26 AM
    Moderator
  • how to get coursesId

    in this line

    foreach(var item in coursesIds)

    {

    }

    and what

      db.Employees.Include is refer it is not work


    Friday, August 26, 2016 9:33 AM
  • any one can reply for me 
    Saturday, August 27, 2016 12:42 PM
  • Hi eng-ahmed-salah,

    Could you please provide related MVC view code, it will be beneficial to resolve the issue.

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, August 29, 2016 4:36 AM
    Moderator