Asp.net mvc 更新代码中的多表关系
我需要更新三个表之间的多对多关系 员工和课程以及员工课程 更新表employeecourse中的课程id时未完成 我的客户雇员模型2:Asp.net mvc 更新代码中的多表关系,asp.net-mvc,linq-to-sql,linq-to-entities,Asp.net Mvc,Linq To Sql,Linq To Entities,我需要更新三个表之间的多对多关系 员工和课程以及员工课程 更新表employeecourse中的课程id时未完成 我的客户雇员模型2: namespace WebCourse.Models { public class Customemployee2 { public string Name { get; set; } public int Salary { get; set; } public string Email { get; set
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 relation in employeecourse where select course id
db.SaveChanges();
return RedirectToAction("Index");
db.Employees.Include
}
return View(cust2);
}
namespace-WebCourse.Models{
公共类Customemployee2
{
公共字符串名称{get;set;}
公共整数{get;set;}
公共字符串电子邮件{get;set;}
public int DistrictId{get;set;}
public int CourseId{get;set;}
公共IEnumerable区域{set;get;}
公共列表课程{get;set;}
公共列表语言{get;set;}
} }
[HttpPost]
公共操作结果编辑(int id,Customemployee2 cust2)
{
Employee old=db.Employees.Find(id);
如果(旧!=null)
{
old.Name=cust2.Name;
old.Email=cust2.Email;
old.DistrictId=cust2.DistrictId;
老.工资=客户2.工资;
//我在这里写的内容是更新employeecourse中的关系,其中选择课程id
db.SaveChanges();
返回操作(“索引”);
db.Employees.Include
}
返回视图(cust2);
}
如何更新表employeecourse中的courseid?您的员工是否有课程集合?如果是,您需要在调用SaveChanges方法之前进行设置。我知道必须在保存更改之前进行设置,但我无法更新表employee Course为什么无法更新?是什么阻止了你?你试过什么?old.EmployeeCourses.Where(m=>!cust2.CourseId.Contains(m.Id)).ToList().ForEach(EmployeeCourse=>old.EmployeeCourses.Remove(EmployeeCourse));问题来了!cust2.CourseId如何在CustomEmployee2模式下表示CourseId您的员工是否有课程集合?如果是,您需要在调用SaveChanges方法之前进行设置。我知道必须在保存更改之前进行设置,但我无法更新表employee Course为什么无法更新?是什么阻止了你?你试过什么?old.EmployeeCourses.Where(m=>!cust2.CourseId.Contains(m.Id)).ToList().ForEach(EmployeeCourse=>old.EmployeeCourses.Remove(EmployeeCourse));问题来了!cust2.CourseId如何在Customemployee2模式下表示CourseId