public ServiceResult <BA04A> Update(BA04A entity) { var errMsg = ""; using (var trans = _Entity.Database.BeginTransaction(IsolationLevel.Snapshot)) { try { if (entity.BA04A_ID == 0) { _Entity.Entry(entity).State = EntityState.Added; } else { _Entity.Entry(entity).State = EntityState.Modified; } var result = _Entity.SaveChanges(); trans.Commit(); CacheHelper.Invalidate("BA04A"); } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { errMsg = ErrorHandler.GetDbEntityValidationExceptionMessage(ex); } catch (Exception ex) { errMsg = ErrorHandler.GetSQLMessage(ex); } } return(new ServiceResult <BA04A>() { Data = entity, Message = errMsg }); }
public static ResultHelper <BA04A> BeforeSave(BA04AModel model, BA04A entity, EntityState state) { var errMsg = ""; if (state == EntityState.Added) //新增 { errMsg = Validation(model); ToEntity(model, ref entity); entity.CREATE_USER = "******"; entity.CREATE_DATE = DateTime.Now; } else if (state == EntityState.Modified)//修改 { errMsg = entity == null ? "資料已被刪除" : Validation(model); ToEntity(model, ref entity); entity.UPDATE_USER = "******"; entity.UPDATE_DATE = DateTime.Now; } else //刪除 { errMsg = entity == null ? "資料已被刪除" : ""; } return(new ResultHelper <BA04A>() { Data = entity, Message = errMsg }); }
public ResultHelper <BA04A> Save(BA04AModel model, BA04A entity, EntityState state) { var result = BA04Business.BeforeSave(model, entity, state); result.Message += _Service.SaveChanges(result.Data, state, result.Message); return(result); }
public static BA04AModel FromEntity(BA04A entity) { var data = new BA04AModel(); if (entity != null) { var objectHelper = new ObjectHelper(); objectHelper.CopyValue(entity, data); } return(data); }
public static void ToEntity(BA04AModel model, ref BA04A entity) { if (entity == null) { entity = new BA04A(); } List <string> exclusiveList = new List <string>() { "CREATE_USER", "CREATE_DATE", "LASTUPDATE_USER", "LASTUPDATE_DATE" }; var objectHelper = new ObjectHelper(); objectHelper.CopyValue(model, entity, exclusiveList); }
/// <summary> /// viewmodel to entity /// </summary> /// <param name="entity"></param> private static BA04A ToEntity(BA04AModel data) { var entity = new BA04A(); if (data != null) { #region [ 資料處理 ] #endregion [ 資料處理 ] var objectHelper = new ObjectHelper(); objectHelper.CopyValue(data, entity); } return(entity); }
public static BA04A BeforSave(BA04AModel data) { var BA04A = new BA04A(); if (data.BA04A_ID == 0)//Adding New { data.CREATE_USER = UserInfo.Id; data.CREATE_DATE = DateTime.Now; } else//Update { var _Service = new BA04Service(); BA04A = _Service.GetA(x => x.BA04A_ID == data.BA04A_ID).First(); data.CREATE_USER = BA04A.CREATE_USER; data.CREATE_DATE = BA04A.CREATE_DATE; data.UPDATE_USER = UserInfo.Id; data.UPDATE_DATE = DateTime.Now; } return(ToEntity(data)); }
/// <summary>刪除</summary> /// <param name="key">Master 的 Key</param> /// <returns>被trigger擋下=> 取回資料, 被其他人刪單=> new BA04A</returns> public ServiceResult <BA04A> Delete(int key) { string errMsg = ""; var master = _Entity.BA04A.FirstOrDefault(x => x.BA04A_ID == key); using (var trans = _Entity.Database.BeginTransaction(IsolationLevel.Snapshot)) { try { if (master != null && master.BA04A_ID > 0) { _Entity.BA04A.Remove(master); _Entity.SaveChanges(); CacheHelper.Invalidate("BA04A"); } else { errMsg = "資料已被刪除";//被其他人刪單 ErrorHandler.GetCodeName("W011"); master = new BA04A(); } trans.Commit(); } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { errMsg = ErrorHandler.GetDbEntityValidationExceptionMessage(ex); } catch (Exception ex) { errMsg = ErrorHandler.GetSQLMessage(ex); } } return(new ServiceResult <BA04A>() { Data = master, Message = errMsg }); }
public string SaveChanges(BA04A entity, EntityState state, string errMsg) { if (errMsg != "") { return(""); } using (var trans = _entity.Database.BeginTransaction(IsolationLevel.Snapshot)) { try { if (state == EntityState.Added) { _Repository.Add(entity); } else if (state == EntityState.Modified) { _Repository.Update(entity); } else if (state == EntityState.Deleted) { _Repository.Delete(entity); } _entity.SaveChanges(); trans.Commit(); } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { errMsg = ex.Message; } catch (Exception ex) { errMsg = ex.Message; } } return(errMsg); }