private void btnSave_Click(object sender, EventArgs e) { bs.EndEdit(); var detailList = (List <TS_DEPT>)bs.DataSource; TL_BASEDATA log = null; foreach (TS_DEPT storeWhse in _db.TS_DEPT) { if (detailList.All(p => p.UID != storeWhse.UID)) { _db.TS_DEPT.Remove(storeWhse); } OperateType logType; string oldValue, newValue; DbEntityEntry <TS_DEPT> entry; try { entry = _db.Entry(storeWhse); } catch (Exception ex) { MessageHelper.ShowInfo(ex.ToString()); bs.DataSource = _db.TS_DEPT.ToList(); return; } var dataType = entry.Entity.GetType().Name; switch (entry.State) { case EntityState.Detached: case EntityState.Unchanged: case EntityState.Added: continue; case EntityState.Deleted: logType = OperateType.Delete; oldValue = GetValues(entry.OriginalValues); newValue = ""; // _db.TS_DEPT.Remove(storeWhse); break; case EntityState.Modified: logType = OperateType.Update; oldValue = GetValues(entry.OriginalValues); newValue = GetValues(entry.CurrentValues); // _db.TS_DEPT.Attach(storeWhse); break; default: throw new ArgumentOutOfRangeException(); } log = BaseDataLogController.Add(_db, dataType, oldValue, newValue, "", logType); } foreach (var detail in detailList.Where(detail => !_db.TS_DEPT.Any(p => p.UID == detail.UID))) { _db.TS_DEPT.Add(detail); var entry = _db.Entry(detail); var dataType = entry.Entity.GetType().Name; var logType = OperateType.Add; var oldValue = ""; var newValue = GetValues(entry.CurrentValues); log = BaseDataLogController.Add(_db, dataType, oldValue, newValue, "", logType); } try { EntitiesFactory.SaveDb(_db); grid.PrimaryGrid.PurgeDeletedRows(); grid.PrimaryGrid.ClearDirtyRowMarkers(); MessageHelper.ShowInfo("保存成功!"); } catch (Exception ex) { MessageHelper.ShowInfo(ex.ToString()); if (log != null) { BaseDataLogController.RemoveLocal(_db, log); } throw; } }
public static void Delete(PowerEntities db, TS_OPERATOR data, TS_OPERATOR oper) { db.Entry(data).State = EntityState.Deleted; }