如题,
例如:
主表班级,数据:
classKey clssName Remark
1 一(1) 班 test
2 一(2 班 test
从表学生,数据:
stuId classKey stuName age
1 1 张三 12
2 1 李四 8
-----------要更新的数据如下
班级班classKey为1的,备注更改成 "Test123"
学生表班级为1的,stuId为1的,age 调整为9
1 一(1) 班 test -----主表
1 1 张三 9 -----从表
var class = context.Classes.FirstOrDefault(x => x.classKey == 1); if(class == null) return; class.Remark = "Test123"; var student = context.Students.FirstOrDefault(x => x.stuId == 1); if(student == null) return; student.age = 9; context.SaveChange();
你好,
如果从表里面有20条数据需要修改,这个怎么做?
我的做法是循环的将对象class.student.Add(part1)保存到这个里面,然后更新,但是我这个更新有问题,主表可以更新,但是从表就不行。
我更新的方法:
/// <summary> /// 修改某一模型数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="model"></param> public void Modify<T>(T model) where T : class { using (dbSupManage db = new dbSupManage()) { if (db.Entry<T>(model).State == EntityState.Detached) { db.Set<T>().Attach(model); db.Entry<T>(model).State = EntityState.Modified; } db.SaveChanges(); } }
数据方法:
[HttpPost] public ActionResult Edit(V_SupManage_SupCycleAssess V_part1) { PublicOP op = new PublicOP(); //--------主表记录信息 SupManage_CycleAssess part1 = new SupManage_CycleAssess() { ReviewID = V_part1.ReviewID, SupManageId = V_part1.SupManageId, SubCompany = V_part1.SubCompany, ReviewDate = V_part1.ReviewDate, //V_part1.ReviewDate, ReviewUser = V_part1.ReviewUser, Score = V_part1.Score, Remark = V_part1.Remark, SaveTime = DateTime.Now, SaveUser = UserID }; //--------从表记录信息 var v = op.GetInfoLst<SupManage_CycleAssessDetail>(c => c.ReviewID == V_part1.ReviewID); SupManage_CycleAssessDetail detail; for (int i = 1; i <= 6; i++) { detail = v.FirstOrDefault(c => c.PeriodicReviewID == i.ToString()); detail.PeriodicReviewVal = Convert.ToDecimal(V_part1.GetType().GetProperty(string.Format("val_{0}", i.ToString())).GetValue(V_part1, null)); part1.SupManage_CycleAssessDetail.Add(detail); } op.Modify<SupManage_CycleAssess>(part1); return Redirect("~/SupCycleAssess/"); }