readonly UnitOfWork _u = new UnitOfWork(); public M Get(int id) { return _u.T_MtnContractRepository().SingleOrDefault(t => t.ID == id); } /// <summary> /// 编辑对应的合同与记录 /// </summary> /// <param name="m"></param> public void Edit(M m) { var dbm = _u.T_MtnContractRepository().GetById(m.ID); var oldIds = dbm.T_MtnContractDetail.Select(a => a.ID).ToList(); var newIds = m.T_MtnContractDetail.Select(a => a.ID).ToList(); oldIds.ForEach(a => { if (newIds.Contains(a)) _u.T_MtnContractDetailRepository().Update(m.T_MtnContractDetail.First(b => b.ID == a)); else { _u.T_MtnContractDetailRepository().Delete(a); } }); m.T_MtnContractDetail.ForEach(a => { a.T_MtnContract_ID = m.ID; if (!oldIds.Contains(a.ID)) _u.T_MtnContractDetailRepository().Insert(a); }); _u.T_MtnContractRepository().Update(m); _u.Save(); }