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)));
        }
Пример #2
0
        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)));
        }
Пример #3
0
        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)));
        }
Пример #4
0
        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 Restore(Employee employee)
 {
     _context.Employees.IgnoreQueryFilters()
     .First(e => e.SSN == employee.SSN &&
            e.FirstName == employee.FirstName &&
            e.FamilyName == employee.FamilyName).SoftDeleted = false;
     _context.SaveChanges();
     return(RedirectToAction(nameof(Index)));
 }
 public IActionResult Update(Employee employee)
 {
     if (_context.Employees.Count(e => e.SSN == employee.SSN &&
                                  e.FirstName == employee.FirstName &&
                                  e.FamilyName == employee.FamilyName) == 0)
     {
         _context.Add(employee);
     }
     else
     {
         Employee e = new Employee
         {
             SSN        = employee.SSN,
             FirstName  = employee.FirstName,
             FamilyName = employee.FamilyName,
             RowVersion = employee.RowVersion
         };
         _context.Employees.Attach(e);
         e.Salary      = employee.Salary;
         e.LastUpdated = DateTime.Now;
     }
     _context.SaveChanges();
     return(RedirectToAction(nameof(Index)));
 }
Пример #7
0
        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)));
        }
Пример #8
0
        public IActionResult Update(Employee employee, decimal originalSalary)
        {
            //Employee existing = context.Employees
            //    .AsTracking()
            //    .First(e => e.SSN == employee.SSN && e.FirstName == employee.FirstName
            //            && e.FamilyName == employee.FamilyName);
            //if (existing == null)
            //{
            //    context.Add(employee);
            //}
            //else
            //{
            //    existing.Salary = employee.Salary;
            //}

            //context.SaveChanges();
            //return RedirectToAction(nameof(Index));

            //if (context.Employees.Count(e => e.SSN == employee.SSN
            //        && e.FirstName == employee.FirstName
            //        && e.FamilyName == employee.FamilyName) == 0)
            //{
            //    context.Add(employee);
            //}
            //else
            //{
            //    context.Update(employee);
            //}
            //Employee existing = context.Employees
            //    .Find(employee.SSN, employee.FirstName, employee.FamilyName);

            //if(existing == null)
            //{
            //    //context.Entry(employee).Property("LastUpdated")
            //    //    .CurrentValue = System.DateTime.Now;
            //    context.Add(employee);
            //}
            //else
            //{
            //    existing.Salary = salary;
            //    context.Entry(existing).Property("LastUpdated")
            //        .CurrentValue = System.DateTime.Now;
            //}
            if (context.Employees.Count(e => e.SSN == employee.SSN &&
                                        e.FirstName == employee.FirstName &&
                                        e.FamilyName == employee.FamilyName) == 0)
            {
                context.Add(employee);
            }
            else
            {
                Employee e = new Employee
                {
                    SSN        = employee.SSN,
                    FirstName  = employee.FirstName,
                    FamilyName = employee.FamilyName,
                    Salary     = originalSalary,
                    RowVersion = employee.RowVersion
                };
                context.Employees.Attach(e);
                e.Salary      = employee.Salary;
                e.LastUpdated = DateTime.Now;
            }

            context.SaveChanges();
            return(RedirectToAction(nameof(Index)));
        }