public static string Update(FORECAST_ORDER fc, TAC_KANBANEntities ceRef = null) { String results = null; bool isSuccess = false; bool isRootTransaction = true; TAC_KANBANEntities ce; if (ceRef == null) { //using new entity object ce = new TAC_KANBANEntities(); } else { //using current entity object ce = ceRef; isRootTransaction = false; } // Start transaction for update a Lease Admin record. try { using (TransactionScope trans = EntityUtils.CreateTransactionScope()) { if (isRootTransaction) { // Manually open connection to prevent EF from auto closing // connection and causing issues with future queries. ce.Connection.Open(); } // Checking for version ce.AttachUpdated(fc); ce.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave); isSuccess = true; trans.Complete(); // Transaction complete } } catch (OptimisticConcurrencyException oex) { // Log error in ELMAH for any diagnostic needs. results = "The current record has been modifed since you have " + "last retrieved it. Please reload the record and " + "attempt to save it again."; } catch (Exception ex) { // Error occurs // Log error in ELMAH for any diagnostic needs. isSuccess = false; results = "An error occurred saving the record. Please try again."; } finally { if (isRootTransaction) { ce.Connection.Dispose(); } } // Finally accept all changes from the transaction. if (isSuccess && isRootTransaction) { ce.Dispose(); } return(results); }
public static String Delete(int id, TAC_KANBANEntities ceRef = null) { String sResult = null; bool isSuccess = false; bool isRootTransaction = true; TAC_KANBANEntities ce; if (ceRef == null) { //using new entity object ce = new TAC_KANBANEntities(); } else { //using current entity object ce = ceRef; isRootTransaction = false; } try { using (TransactionScope trans = EntityUtils.CreateTransactionScope()) { if (isRootTransaction) { // Manually open connection to prevent EF from auto closing // connection and causing issues with future queries. ce.Connection.Open(); } var forecastDetail = from com in ce.FORECAST_ORDER_DETAIL where com.FORCAST_ID == id select com; if (forecastDetail.Count() > 0) { foreach (FORECAST_ORDER_DETAIL item in forecastDetail.ToList <FORECAST_ORDER_DETAIL>()) { ce.DeleteObject(item); } } //Delete record var forecast = from doc in ce.FORECAST_ORDER where doc.ID == id select doc; if (forecast.Any()) { FORECAST_ORDER fc = forecast.FirstOrDefault(); ce.FORECAST_ORDER.DeleteObject(fc); } else { throw new NullReferenceException(); } if (isRootTransaction) { // We tentatively save the changes so that we can finish // processing the transaction.foreach (var bk in books) ce.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave); } isSuccess = true; trans.Complete(); //Transaction complete. } } catch (Exception ex) { // Log error in ELMAH for any diagnostic needs. sResult = ex.ToString(); isSuccess = false; } finally { if (isRootTransaction) { ce.Connection.Dispose(); } } // Finally accept all changes from the transaction. if (isSuccess && isRootTransaction) { ce.AcceptAllChanges(); ce.Dispose(); } return(sResult); }