/// <summary> /// 建立檔案與請假單的關聯 /// </summary> /// <param name="leaveId"></param> /// <param name="googleDriveFileId"></param> /// <returns></returns> private void SetLeaveFileReference(int leaveId, string[] googleDriveFileId) { var result = new LeaveFile(); using (var db = _uow.DbContext) { using (var dbTransaction = db.Database.BeginTransaction()) { try { foreach (var _item in googleDriveFileId) { var _entity = new LeaveFile(); _entity.LeaveId = leaveId; var _file = db.GoogleFile.Where(t => t.FileId == _item.ToString()).FirstOrDefault(); _entity.GoogleId = _file.Id; db.LeaveFile.Add(_entity); } //執行 db.SaveChanges(); dbTransaction.Commit(); } catch (Exception ex) { dbTransaction.Rollback(); throw ex; } } } }
/// <summary> /// 建立請假單與檔案的關聯 /// </summary> /// <param name="leaveId"></param> /// <param name="files"></param> private void SetLeaveFileReferenceByFiles(int leaveId, List <FileStorageViewModel> files) { var db = _uow.DbContext; try { foreach (var file in files) { var leaveFileEntity = new LeaveFile(); leaveFileEntity.LeaveId = leaveId; leaveFileEntity.GoogleId = file.Id; db.LeaveFile.Add(leaveFileEntity); } db.SaveChanges(); } catch (Exception ex) { throw ex; } }