Exemple #1
0
 static void SaveChangesInPostgres <T>(IList <T> list, DbContextOptionsBuilder <RPCSContext> dbContextOptionsBuilderPostgres) where T : class
 {
     using (var rpcsContextPostgres = new RPCSContext(dbContextOptionsBuilderPostgres.Options))
     {
         rpcsContextPostgres.Set <T>().Clear();
         rpcsContextPostgres.SaveChanges();
         foreach (var item in list)
         {
             rpcsContextPostgres.Add(item);
         }
         rpcsContextPostgres.SaveChanges();
     }
 }
Exemple #2
0
        static void SaveChangesInPostgres(IList <Employee> employees, IList <Department> departments, DbContextOptionsBuilder <RPCSContext> dbContextOptionsBuilderPostgres)
        {
            var saveListEmployee    = new List <Employee>();
            var saveListDepartments = new List <Department>();

            using (var rpcsContextPostgres = new RPCSContext(dbContextOptionsBuilderPostgres.Options))
            {
                rpcsContextPostgres.Set <Employee>().Clear();
                rpcsContextPostgres.Set <Department>().Clear();
                rpcsContextPostgres.SaveChanges();

                foreach (var item in employees)
                {
                    saveListEmployee.Add(new Employee()
                    {
                        ID = item.ID, DepartmentID = item.DepartmentID
                    });
                    item.DepartmentID = null;
                    rpcsContextPostgres.Add(item);
                }
                rpcsContextPostgres.SaveChanges();

                foreach (var item in departments)
                {
                    saveListDepartments.Add(new Department()
                    {
                        ID = item.ID,
                        DepartmentManagerID          = item.DepartmentManagerID,
                        DepartmentManagerAssistantID = item.DepartmentManagerAssistantID,
                        DepartmentPAID = item.DepartmentPAID
                    });
                    item.DepartmentManagerID          = null;
                    item.DepartmentManagerAssistantID = null;
                    item.DepartmentPAID = null;
                    rpcsContextPostgres.Add(item);
                }
                rpcsContextPostgres.SaveChanges();


                //https://stackoverflow.com/questions/40073149/entity-framework-circular-dependency-for-last-entity
                //using (var scope = new TransactionScope())
                //{
                //    foreach (var item in firstList)
                //    {
                //        rpcsContextPostgres.Add(item);
                //    }
                //    rpcsContextPostgres.SaveChanges();
                //    scope.Complete();
                //}

                //using (var scope = new TransactionScope())
                //{
                //    foreach (var item in secondList)
                //    {
                //        rpcsContextPostgres.Add(item);
                //    }
                //    rpcsContextPostgres.SaveChanges();
                //    scope.Complete();
                //}
            }

            using (var rpcsContextPostgres = new RPCSContext(dbContextOptionsBuilderPostgres.Options))
            {
                //обновление пустых полей
                foreach (var employee in saveListEmployee)
                {
                    rpcsContextPostgres.Employees.Attach(employee);
                    rpcsContextPostgres.Entry(employee).Property(x => x.DepartmentID).IsModified = true;
                }
                rpcsContextPostgres.SaveChanges();


                foreach (var department in saveListDepartments)
                {
                    rpcsContextPostgres.Departments.Attach(department);
                    rpcsContextPostgres.Entry(department).Property(x => x.DepartmentManagerID).IsModified          = true;
                    rpcsContextPostgres.Entry(department).Property(x => x.DepartmentManagerAssistantID).IsModified = true;
                    rpcsContextPostgres.Entry(department).Property(x => x.DepartmentPAID).IsModified = true;
                }
                rpcsContextPostgres.SaveChanges();
            }
        }