public ActionResult UpdateAll(Employee[] employees) { //foreach (Employee employee in employees) //{ // try // { // advancedContext.Update(employee); // advancedContext.SaveChanges(); // } // catch (Exception ex) // { // advancedContext.Entry(employee).State = Microsoft.EntityFrameworkCore.EntityState.Detached; // } //} //advancedContext.UpdateRange(employees); //advancedContext.SaveChanges(); advancedContext.Database.BeginTransaction(); try { advancedContext.UpdateRange(employees); advancedContext.SaveChanges(); advancedContext.Database.CommitTransaction(); } catch (Exception ex) { advancedContext.Database.RollbackTransaction(); } return(RedirectToAction(nameof(Index))); }
public IActionResult UpdateAll(Employee[] employees) { context.Database.BeginTransaction(level); context.UpdateRange(employees); Employee temp = new Employee { SSN = "00-00-0000", FirstName = "Temporary", FamilyName = "Row", Salary = 0 }; context.Add(temp); context.SaveChanges(); System.Threading.Thread.Sleep(5000); context.Remove(temp); context.SaveChanges(); if (context.Employees.Sum(e => e.Salary) < 1_000_000) { context.Database.CommitTransaction(); } else { context.Database.RollbackTransaction(); logger.LogError("Salary total exceeds limit"); } return(RedirectToAction(nameof(Index))); }
public IActionResult UpdateAll(Employee[] employees) { // NOTE: Transaction class implements Disposable pattern and can be used via using. // A transaction is automatically rolled back when it is disposed. Example: // using (var transaction = context.Database.BeginTransaction()) { // ...operations are performed... // context.Database.CommitTransaction(); // } // NOTE: Transaction isolation levels: // - Chaos - This value is poorly defined, but when it is supported, it typically // behaves as though transactions are disabled.Changes made to the // database are visible to other clients before they are committed, and // there is often no rollback support. SQL Server does not support this // isolation level. // - ReadUncomitted - This value represents the lowest level of isolation that is // commonly supported. Transactions using this isolation level // can read changes made by other transactions that have not // been committed. // - ReadComitted - This value is the default level of isolation that is used if // no value is specified.Other transactions can still insert or // delete data between the updates made by the current // transaction, which can result in inconsistent query results. // - RepeatableRead - This value represents a higher level of isolation that // prevents other transactions from modifying data that the // current transaction has read, which ensures consistent // query results. // - Serializable - This value increases the RepeatableRead isolation level by // preventing other transactions from adding data to regions of // the database that have been read by the current transaction. // - Snapshot - This value represents the highest level of isolation and ensures // that transactions each work with their own data snapshot. This // isolation level requires a change to the database that cannot be // performed using EF Core for current moment. _context.Database.BeginTransaction(); // isolationLevel argument values and extension // is method declared in // Microsoft.EntityFrameworkCore namespace //_context.Database.CurrentTransaction can be used to access current transaction _context.UpdateRange(employees); _context.SaveChanges(); if (_context.Employees.Sum(e => e.Salary) < 1_000_000) { try { _context.Database.CommitTransaction(); } catch (Exception) { _context.Database.RollbackTransaction(); } } else { _context.Database.RollbackTransaction(); throw new Exception("Salary total exceeds limit"); } return(RedirectToAction(nameof(Index))); }
public IActionResult UpdateAll(Employee[] employees) { context.Database.BeginTransaction(level); context.UpdateRange(employees); Employee temp = new Employee { SSN = "00-00-0000", FirstName = "Temporary", FamilyName = "Row", Salary = 0 }; context.Add(temp); context.SaveChanges(); System.Threading.Thread.Sleep(5000); context.Remove(temp); context.SaveChanges(); if (context.Employees.Sum(e => e.Salary) < 1_000_000) { context.Database.CommitTransaction(); } else { context.Database.RollbackTransaction(); throw new Exception("Salary total exceeds limit"); } //try //{ // context.UpdateRange(employees); // context.SaveChanges(); // context.Database.CommitTransaction(); //} //catch (Exception) //{ // context.Database.RollbackTransaction(); //} //foreach (Employee e in employees) //{ // try // { // context.Update(e); // context.SaveChanges(); // } // catch (Exception) // { // context.Entry(e).State = EntityState.Detached; // } //} return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> UpdateRangeAsync(Employee[] employees) { await context.Database.BeginTransactionAsync(IsolationLevel.Serializable); context.UpdateRange(employees); await context.SaveChangesAsync(); if (await context.Employees.SumAsync(e => e.Salary) > 10000) { await context.Database.RollbackTransactionAsync(); } else { await context.Database.CommitTransactionAsync(); } return(Ok()); }
public IActionResult UpdateAll(Employee[] employees) { //context.UpdateRange(employees); //context.SaveChanges(); //her bir güncelleme için ayrı transaction çalıştırmak için: //foreach (Employee e in employees) //{ // try // { // context.Update(e); // context.SaveChanges(); // } // catch (Exception) // { // context.Entry(e).State = EntityState.Detached; // } //} //context.Database.BeginTransaction(); //Otomatik transaction kapalı olduğu durumlarda grup güncellemesi için transaction ef ile açılabilir. //try //{ // context.UpdateRange(employees); // context.SaveChanges(); // Burada henüz db de değişiklik yoktur. transaction commit edildikten sonra db değişecektir. // if (context.Employees.Sum(e => e.Salary) < 1_000_000) // { // context.Database.CommitTransaction(); // } // else // { // context.Database.RollbackTransaction(); // throw new Exception("Salary total exceeds limit"); // } // context.Database.CommitTransaction(); //} //catch (Exception) //{ // context.Database.RollbackTransaction(); //} //context.Database.BeginTransaction(); //context.UpdateRange(employees); //context.SaveChanges(); //if (context.Employees.Sum(e => e.Salary) < 1_000_000) //{ // context.Database.CommitTransaction(); //} //else //{ // context.Database.RollbackTransaction(); // throw new Exception("Salary total exceeds limit"); //} context.Database.BeginTransaction(level); context.UpdateRange(employees); Employee temp = new Employee { SSN = "00-00-0000", FirstName = "Temporary", FamilyName = "Row", Salary = 0 }; context.Add(temp); context.SaveChanges(); System.Threading.Thread.Sleep(5000); context.Remove(temp); context.SaveChanges(); if (context.Employees.Sum(e => e.Salary) < 1_000_000) { context.Database.CommitTransaction(); } else { context.Database.RollbackTransaction(); logger.LogError("Salary total exceeds limit"); } return(RedirectToAction(nameof(Index))); }