///<summary>Saves all context changes</summary> public StoredProcResult Save() { try { this.Context.SubmitChanges(); } //handle primary key violation by removing already existing records and trying to insert the other models //so that if there are multiple models received, the correct ones will still be added to database catch (System.Data.SqlClient.SqlException sqlEx) { SqlExceptionCode exceptionCode = (SqlExceptionCode)Enum.ToObject(typeof(SqlExceptionCode), sqlEx.Number); switch (exceptionCode) { case SqlExceptionCode.Deadlock: { //avoid infinite recursion if retry count has reached limit if (saveRetryCount >= MAX_RETRY_COUNT) { return(StoredProcResult.Error); } System.Threading.Thread.Sleep(500); saveRetryCount++; Logger.Log(sqlEx); return(this.Save()); } default: { Logger.Log(sqlEx); return(StoredProcResult.Error); } } } //handle concurrency exceptions catch (ChangeConflictException changeConflictEx) { //avoid infinite recursion if retry count has reached limit if (saveRetryCount >= MAX_RETRY_COUNT) { return(StoredProcResult.ErrChanged); } Logger.Log(changeConflictEx); //resolve concurrency issue by retrieving only changed fields into conflicting entities //and try saving again foreach (ObjectChangeConflict changeConflict in this.Context.ChangeConflicts) { changeConflict.Resolve(System.Data.Linq.RefreshMode.KeepCurrentValues); } this.saveRetryCount += 1; //retry saving changes this.Save(); } catch (Exception ex) { Logger.Log(ex); return(StoredProcResult.Error); } return(StoredProcResult.Success); }
public static BusinessException TryExtractBusinessException(SqlExceptionCode sqlExceptionCode) { return(BusinessExceptionMappingTable.ContainsKey(sqlExceptionCode) ? BusinessExceptionMappingTable[sqlExceptionCode] : new SqlUnknownException()); }