日期:2014-05-20 浏览次数:20603 次
/// <summary> /// 更新由反序列化后得到的“离线”实体 TMaster。 /// 首先要先得到 子表 的哪些主键是新增的、更改的、删除的 /// </summary> /// <param name="detachedMaster"></param> /// <param name="insertedIds"></param> /// <param name="updatedIds"></param> /// <param name="deletedIds"></param> /// <returns></returns> bool UpdateDetachedMaster(TMaster detachedMaster, int[] insertedIds, int[] updatedIds, int[] deletedIds) { using (DbDataContext db = new DbDataContext( connection ) { foreach (int detailId in insertedIds) { db.TDetail.InsertOnSubmit(detachedMaster.TDetail.First(detail => detail.detailId == detailId)); } foreach (int detailId in updatedIds) { db.TDetail.Attach(detachedMaster.TDetail.First(detail => detail.detailId == detailId), true); } foreach (int detailId in deletedIds) { db.TDetail.DeleteOnSubmit(detachedMaster.TDetail.First(detail => detail.detailId == detailId)); } db.TMaster.Attach(detachedMaster, true); // 可以获取 db.GetChangeSet() 进行日志操作等; db.SubmitChanges(); } return true; }