Exemplo n.º 1
0
        //below is the scenario when the restaurant is not tracked
        private static void UpdateRestaurantWhenNotTracked()
        {
            var restaurant = context.Restaurants.Find(1);

            restaurant.Name = "Restaurant 1";
            using (var context1 = new RestaurantDbContext())
            {
                context1.Restaurants.Update(restaurant);
                context1.SaveChanges();
            }
        }
Exemplo n.º 2
0
        private static void UpdateRestaurantWithFoodItemsIncludedNotTracked()
        {
            var restaurant = context.Restaurants.Include(t => t.FoodItems).FirstOrDefault(t => t.Id == 7);

            restaurant.Name = "Restaurant 1";
            using (var context1 = new RestaurantDbContext())
            {
                // this works fine since it has no navigation property to any other entity
                context1.Restaurants.Update(restaurant);
                //In case there was navigation property to any other entity then do the following
                //context1.Entry(restaurant).State = EntityState.Modified;
                context1.SaveChanges();
            }
        }
Exemplo n.º 3
0
        private static void UpdateFoodItemWithIncludeRestaurantNotTracked()
        {
            var foodItem = context.FoodItems.Include(t => t.Restaurant).FirstOrDefault(t => t.Id == 1);

            foodItem.Name = "FoodItem 1 updated";
            using (var context1 = new RestaurantDbContext())
            {
                // this is not efficient; it has a navigation property and so the untracked update with lead to
                //updating the navigation field as well here restaurant
                //context1.FoodItems.Update(foodItem);

                //Since we have navigation property to any other entity do the following
                //now only update for food item will be sent and not for restaurant
                context1.Entry(foodItem).State = EntityState.Modified;
                context1.SaveChanges();
            }
        }