Ejemplo n.º 1
0
        // атрибуты Diet добавить при изминении  update diet
        public static void UpdateDiet(this Diet newdiet, int[] arr)
        {
            using (var db = new GoodFit())
            {
                Diet diet = db.Diet.FirstOrDefault(j => j.Id == newdiet.Id);
                diet.Name         = newdiet.Name;
                diet.Subscription = newdiet.Subscription;
                diet.Description  = newdiet.Description;
                if (arr.Length == 0)
                {
                    db.Diet.FirstOrDefault(j => j.Id == newdiet.Id).Dish.Clear();
                    db.SaveChanges();
                    return;
                }

                foreach (var item in diet.Dish.ToList())
                {
                    if (arr.Contains(item.id))
                    {
                    }
                    else
                    {
                        db.Diet.FirstOrDefault(j => j.Id == newdiet.Id).Dish.Remove(db.Dish.FirstOrDefault(i => i.id == item.id));
                    }
                }
                db.SaveChanges();

                diet.AddDishes(arr);

                db.SaveChanges();
            }



            //db.Entry(diet).State = EntityState.Modified;
            //diet.Dish.Clear();


            //  db.Diet.FirstOrDefault(j => j.Id == diet.Id)
        }