title: Update only works in debug mode
body: <p>I'm new to using entity as a data layer between MVC and SQL Server, so I apologize up front if what I'm doing is bad practice. </p>
  
  <p>Let me start by sharing the code that is handling the update.</p>
  
  <p>Update Delivery:</p>
  
  <pre><code>public bool One(Delivery toUpdate)
  {
      using (var dbContext = new FDb())
      {
          try
          {
              var deliveryInDb = this.dbTable(dbContext).Single(x =&gt; x.DeliveryId == toUpdate.DeliveryId);
  
              dbContext.Entry(deliveryInDb).CurrentValues.SetValues(toUpdate);
  
              //removal first
              List&lt;DeliveryDay&gt; currentDays = FEngineCore.DeliveryDay.Get.ForValue((x =&gt; x.DeliveryId), toUpdate.DeliveryId);
              List&lt;DeliveryTime&gt; currentTimes = FEngineCore.DeliveryTime.Get.ForValue((x =&gt; x.DeliveryId), toUpdate.DeliveryId);
  
              //remove delivery days that are not needed
              foreach (var curDay in currentDays)
              {
                  if (!toUpdate.DeliveryDays.Select(x =&gt; x.DeliveryDayId).Contains(curDay.DeliveryDayId))
                  {
                      FEngineCore.DeliveryDay.Delete.One((x =&gt; x.DeliveryDayId), curDay.DeliveryDayId);
                      deliveryInDb.DeliveryDays.Remove(curDay);
                  }
              }
  
              //remove delivery times that are not needed
              foreach (var curTime in currentTimes)
              {
                  if (!toUpdate.DeliveryTimes.Select(x =&gt; x.DeliveryTimeId).Contains(curTime.DeliveryTimeId))
                  {
                      FEngineCore.DeliveryTime.Delete.One((x =&gt; x.DeliveryTimeId), curTime.DeliveryTimeId);
                      deliveryInDb.DeliveryTimes.Remove(curTime);
                  }
              }
  
              foreach (var day in toUpdate.DeliveryDays)
              {
                  if (day.DeliveryDayId == 0)
                  {
                      dbContext.DeliveryDays.Add(day);
                  }
                  else
                  {
                      if (dbContext.DeliveryDays.Local.Any(e =&gt; e.DeliveryDayId == day.DeliveryDayId))
                      {
                          dbContext.Entry(dbContext.DeliveryDays.Local.First(e =&gt; e.DeliveryDayId == day.DeliveryDayId)).CurrentValues.SetValues(day);
                          dbContext.Entry(dbContext.DeliveryDays.Local.First(e =&gt; e.DeliveryDayId == day.DeliveryDayId)).State = EntityState.Modified;
                      }
                      else
                      {
                          DeliveryDay modDay = new DeliveryDay
                          {
                              DayOfWeek = day.DayOfWeek,
                              DeliveryDayId = day.DeliveryDayId,
                              DeliveryId = day.DeliveryId,
                              Interval = day.Interval
                          };
  
                          dbContext.DeliveryDays.Attach(modDay);
                          dbContext.Entry(modDay).State = EntityState.Modified;
                      }
  
                      deliveryInDb.DeliveryDays.Add(day);
                  }
              }
  
              foreach (var time in toUpdate.DeliveryTimes)
              {
                  if (time.DeliveryTimeId == 0)
                  {
                      dbContext.DeliveryTimes.Add(time);
                  }
                  else
                  {
                      if (dbContext.DeliveryTimes.Local.Any(e =&gt; e.DeliveryTimeId == time.DeliveryTimeId))
                      {
                          dbContext.Entry(dbContext.DeliveryTimes.Local.First(e =&gt; e.DeliveryTimeId == time.DeliveryTimeId)).CurrentValues.SetValues(time);
                          dbContext.Entry(dbContext.DeliveryTimes.Local.First(e =&gt; e.DeliveryTimeId == time.DeliveryTimeId)).State = EntityState.Modified;
                      }
                      else
                      {
                          DeliveryTime modTime = new DeliveryTime
                          {
                              DeliveryId = time.DeliveryId,
                              DeliveryLocationId = time.DeliveryLocationId,
                              DeliveryTimeId = time.DeliveryTimeId,
                              DropoffTime = time.DropoffTime
                          };
  
                          dbContext.DeliveryTimes.Attach(modTime);
                          dbContext.Entry(modTime).State = EntityState.Modified;
                      }
  
                      deliveryInDb.DeliveryTimes.Add(time);
                  }
              }
  
              dbContext.SaveChanges();
              dbContext.Entry(deliveryInDb).State = EntityState.Detached;
  
              return true;
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.InnerException);
              return false;
          }
      }
  }
  </code></pre>
  
  <p>Let me continue by explaining that the delivery object has 2 children; <em>DeliveryTime</em> and <em>DeliveryDay</em>. The issue that arises happens when I try to remove one <em>deliveryTime</em> and modify nothing else. The end result of running the code normally (not in debug) is that the <em>deliveryTime</em> is in fact <em>not</em> removed. Here's the interesting thing guys, when I debug it and go through the break points, everything works as expected! </p>
  
  <p>Let me continue by posting the code that is running behind the removal method of the <em>deliveryTime</em> (actually all entity objects in my system).</p>
  
  <pre><code>public bool One&lt;V&gt;(Expression&lt;Func&lt;T, V&gt;&gt; property, V value) where V : IComparable
  {
      using (var dbContext = new FoodsbyDb())
      {
          try
          {
              T toDelete; 
  
              //get the body as a property that represents the property of the entity object
              MemberExpression entityPropertyExpression = property.Body as MemberExpression;
  
              //get the parameter that is representing the entity object
              ParameterExpression entityObjectExpression = (ParameterExpression)entityPropertyExpression.Expression;
  
              //represent the value being checked against as an expression constant
              Expression valueAsExpression = Expression.Constant(value);
  
              //check the equality of the property and the value
              Expression equalsExpression = Expression.Equal(entityPropertyExpression, valueAsExpression);
  
              //create an expression that takes the entity object as a parameter, and checks the equality using the equalsExpression variable
              Expression&lt;Func&lt;T, bool&gt;&gt; filterLambda = Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(equalsExpression, entityObjectExpression);
  
              toDelete = this.dbTable(dbContext)
                  .SingleOrDefault(filterLambda);
  
              if (toDelete != null)
              {
                  this.dbTable(dbContext)
                  .Remove(toDelete);
  
                  dbContext.SaveChanges();
  
                  return true;
              }
  
              return false;
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.InnerException);
              return false;
          }
      }
  }
  </code></pre>
  
  <p>The code above is obviously generic, and it handles all my entity objects. I have tested it in and out and know for sure the problem does not lie in there. I thought it would be helpful to post it so you all can have a full understanding of what's going on. </p>
  
  <p>Here's my best guess as to what's going on:</p>
  
  <p>The reference to the removed <em>deliveryTime</em> still exists when the database context is saved, <em>but</em> when I debug, the system has enough time to remove the context. </p>
  
  <p>Here was one of my attempted solutions:</p>
  
  <p>Remove all references to the children objects <em>immediately</em> after setting <em>currentDays</em> and <em>currentTimes</em> and then proceeding to add them back to <em>deliveryInDb</em> as you enumerate through them.</p>
  
  <p>Because I am new to all of this, if you see some bad practice along with the solution, I wouldn't mind constructive criticism to improve my programming method.</p>