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 async Task <bool> DeleteAsync(Employee employee)
        {
            var employeeDB = await context.Employees
                             .Include(e => e.OtherIdentity)
                             .FirstOrDefaultAsync(e => e.SSN == employee.SSN && e.FirstName == employee.FirstName && e.FamilyName == employee.FamilyName);

            if (employeeDB == null)
            {
                return(false);
            }

            context.Remove(employeeDB);
            var deleted = await context.SaveChangesAsync() > 0;

            return(deleted);
        }
Exemple #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)));
        }
Exemple #4
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)));
        }