public static List <Catalog_level> UpdateCatalogLevel(CatalogLevelViewModel viewModel)
        {
            using (var connect = new Catalog_Level_WPFContainer())
            {
                // отключим авто отслеживание изменений в БД для улучшения производительности
                connect.Configuration.AutoDetectChangesEnabled = false;

                foreach (var item in connect.Catalog_level)
                {
                    if (item.ID == viewModel.ID)
                    {
                        item.Name = viewModel.Name;
                    }
                }

                // обнаружим изменения
                connect.ChangeTracker.DetectChanges();

                connect.SaveChanges();

                var catalogLevels = connect.Catalog_level.ToList();

                return(catalogLevels);
            }
        }
        public static List <Catalog_level> AddCatalogLevel(CatalogLevelViewModel viewModel, string valueTextBox)
        {
            using (var connect = new Catalog_Level_WPFContainer())
            {
                connect.Configuration.AutoDetectChangesEnabled = false;

                connect.Catalog_level.Add(new Catalog_level {
                    Name = valueTextBox, ParentID = viewModel.ID
                });

                connect.ChangeTracker.DetectChanges();

                connect.SaveChanges();

                var catalogLevels = connect.Catalog_level.ToList();

                return(catalogLevels);
            }
        }
        public static List <Catalog_level> RemoveCatalogLevel(int id)
        {
            using (var connect = new Catalog_Level_WPFContainer())
            {
                connect.Configuration.AutoDetectChangesEnabled = false;

                var catalogLevels = connect.Catalog_level.ToList();

                foreach (var item in catalogLevels)
                {
                    if (id == item.ID)
                    {
                        connect.Catalog_level.Remove(item);
                        connect.ChangeTracker.DetectChanges();
                        connect.SaveChanges();
                    }
                }

                var catalogLevelsNew = connect.Catalog_level.ToList();

                return(catalogLevelsNew);
            }
        }
        public static void Seed(Catalog_Level_WPFContainer connect)
        {
            var catalogs = new List <Catalogs>()
            {
                new Catalogs {
                    Name = "VOLVO"
                },
                new Catalogs {
                    Name = "ER"
                }
            };

            catalogs.ForEach(x => connect.Catalogs.AddOrUpdate(c => new { c.Name }, x));

            var aggregates = new List <Aggregates>()
            {
                new Aggregates {
                    Name = "КПП", Catalogs = catalogs[0]
                },
                new Aggregates {
                    Name = "Двигатель", Catalogs = catalogs[1]
                },
                new Aggregates {
                    Name = "КПП", Catalogs = catalogs[1]
                }
            };

            aggregates.ForEach(x => connect.Aggregates.AddOrUpdate(a => new { a.Name, a.CatalogID }, x));

            var models = new List <Models>()
            {
                new Models {
                    Name = "A365", Aggregates = aggregates[0]
                },
                new Models {
                    Name = "M4566", Aggregates = aggregates[1]
                },
                new Models {
                    Name = "FG4511", Aggregates = aggregates[1]
                },
                new Models {
                    Name = "T45459", Aggregates = aggregates[2]
                }
            };

            models.ForEach(x => connect.Models.AddOrUpdate(m => new { m.Name, m.AggregateID }, x));
            connect.SaveChanges();

            var sampleCatalogs = connect.Catalogs.ToList();

            foreach (var item in sampleCatalogs)
            {
                cl_Consol.Add(new CL_Consolidation
                {
                    ID         = (cl_Consol.Count) + 1,
                    Name       = item.Name,
                    Catalog_ID = item.ID
                });
            }

            var sampleAggregates = connect.Aggregates.ToList();

            foreach (var item in sampleAggregates)
            {
                cl_Consol.Add(new CL_Consolidation
                {
                    ID                   = (cl_Consol.Count) + 1,
                    Name                 = item.Name,
                    Aggregate_ID         = item.ID,
                    Aggregate_catalog_ID = item.CatalogID
                });
            }

            var sampleModels = connect.Models.ToList();

            foreach (var item in sampleModels)
            {
                cl_Consol.Add(new CL_Consolidation
                {
                    ID   = (cl_Consol.Count) + 1,
                    Name = item.Name,
                    Model_aggregate_ID = item.AggregateID
                });
            }

            var selectCatalogsFromCl_Consol = from cl in cl_Consol
                                              where cl.Catalog_ID != null
                                              select new
            {
                CLC_ID         = cl.ID,
                CLC_Parent_ID  = cl.Parent_ID,
                CLC_Name       = cl.Name,
                CLC_Catalog_ID = cl.Catalog_ID
            };

            var selectAggregatesFromCl_Consol = from cl in cl_Consol
                                                join c in selectCatalogsFromCl_Consol
                                                on cl.Aggregate_catalog_ID equals c.CLC_Catalog_ID
                                                where cl.Aggregate_catalog_ID != null
                                                select new
            {
                CLA_ID           = cl.ID,
                CLA_Parent_ID    = c.CLC_ID,
                CLA_Name         = cl.Name,
                CLA_Aggregate_ID = cl.Aggregate_ID
            };

            var selectModelsFromCl_Consol = from cl in cl_Consol
                                            join a in selectAggregatesFromCl_Consol
                                            on cl.Model_aggregate_ID equals a.CLA_Aggregate_ID
                                            where cl.Model_aggregate_ID != null
                                            select new
            {
                CLM_ID        = cl.ID,
                CLM_Parent_ID = a.CLA_ID,
                CLM_Name      = cl.Name
            };

            foreach (var item in selectCatalogsFromCl_Consol)
            {
                connect.Catalog_level.Add(new Catalog_level
                {
                    ID       = item.CLC_ID,
                    ParentID = item.CLC_Parent_ID,
                    Name     = item.CLC_Name
                });
            }

            foreach (var item in selectAggregatesFromCl_Consol)
            {
                connect.Catalog_level.AddOrUpdate(new Catalog_level
                {
                    ID       = item.CLA_ID,
                    ParentID = item.CLA_Parent_ID,
                    Name     = item.CLA_Name
                });
            }

            foreach (var item in selectModelsFromCl_Consol)
            {
                connect.Catalog_level.AddOrUpdate(new Catalog_level
                {
                    ID       = item.CLM_ID,
                    ParentID = item.CLM_Parent_ID,
                    Name     = item.CLM_Name
                });
            }

            connect.SaveChanges();
        }