/// <summary> /// 保存or更新数据对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dto">实体对象</param> /// <param name="select">查询属性</param> public int Set <T>(T dto, Expression <Func <T, object> > select = null) where T : class, new() { ExHelper.ThrowIfNull(dto, "操作对象不能为空."); int rowCount = 0; DtoMapping dtoDbMapping = DtoMappingHelper.GetDtoMapping <T>(); object pkValue = dtoDbMapping.PkMap.Pi.GetValue(dto); if (ExHelper.IsNullOrEmpty(pkValue)) { rowCount = Add(dto, select); } else { rowCount = Update(dto, select); } return(rowCount); }
/// <summary> /// 批量保存数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="list">实体对象</param> /// <param name="select">查询属性</param> /// <param name="useTransaction">使用事务操作 默认false</param> /// <param name="progress">保存进度</param> public int SetList <T>(List <T> list, Expression <Func <T, object> > select = null, bool useTransaction = false, IProgress <double> progress = null) where T : class, new() { if (list == null || list.Count <= 0) { return(0); } int rowCount = 0; DtoMapping dtoDbMapping = DtoMappingHelper.GetDtoMapping <T>(); List <T> addList = new List <T>(); List <T> updateList = new List <T>(); #region 拆分新增或更新对象List for (int i = 0; i < list.Count; i++) { object pkValue = dtoDbMapping.PkMap.Pi.GetValue(list[i]); if (ExHelper.IsNullOrEmpty(pkValue)) { addList.Add(list[i]); } else { updateList.Add(list[i]); } } #endregion if (useTransaction) { if (addList.Count > 0 && updateList.Count > 0) { throw new Exception("插入和更新操作同时存在时,不能启用事务."); } } if (addList.Count > 0) { rowCount += AddList(addList, select, useTransaction, progress); } if (updateList.Count > 0) { rowCount += UpdateList(updateList, select, useTransaction, progress); } return(rowCount); }