title: Entity Framework In Memory Database provider weird behavior
body: <pre><code>[HttpGet]
  public JsonResult GetTodoItems()
  {
      return new JsonResult(_context.TodoItems.ToList());
  }
  
  [HttpPost]
  public JsonResult PostTodoItem(TodoItem item)
  {
      _context.TodoItems.Add(item);
      _context.SaveChanges();
      return new JsonResult(&quot;Added successfully!&quot;);
  }
  
  [HttpDelete(&quot;{id}&quot;)]
  public JsonResult DeleteTodoItem(long id)
  {
      _context.TodoItems.Remove(_context.TodoItems.Find(id));
      _context.SaveChanges();
      return new JsonResult(&quot;Deleted successfully!&quot;);
  }
  </code></pre>
  <p>Also, here is the TodoItem model:</p>
  <pre><code>public class TodoItem
  {
      public long Id { get; set; }
      public string Name { get; set; }
      public bool IsComplete { get; set; }
  }
  </code></pre>
  <p>For some reason, I do not understand what goes behind the scenes in the following scenario:</p>
  <p>I delete one of the items using the API. And if I now add another item onto the list, the order of TodoItems seems to change, and was wondering what goes behind the scene to invoke such behavior.</p>
  <p>Here is an example - this is the starting situation:</p>
  <pre><code>[
      { &quot;Id&quot;: 1, &quot;Name&quot;: &quot;Item1&quot;, &quot;IsComplete&quot;: false },    
      { &quot;Id&quot;: 2, &quot;Name&quot;: &quot;Item2&quot;, &quot;IsComplete&quot;: false },
      { &quot;Id&quot;: 3, &quot;Name&quot;: &quot;Item3&quot;, &quot;IsComplete&quot;: false }
  ]
  </code></pre>
  <p>Now I delete Item2 then the list returned by the API is:</p>
  <pre><code>[
      { &quot;Id&quot;: 1, &quot;Name&quot;: &quot;Item1&quot;, &quot;IsComplete&quot;: false },
      { &quot;Id&quot;: 3, &quot;Name&quot;: &quot;Item3&quot;, &quot;IsComplete&quot;: false}
  ]
  </code></pre>
  <p>Then I add Item4 then the list is:</p>
  <pre><code>[
      { &quot;Id&quot;: 1, &quot;Name&quot;: &quot;Item1&quot;, &quot;IsComplete&quot;: false },
      { &quot;Id&quot;: 4, &quot;Name&quot;: &quot;Item4&quot;, &quot;IsComplete&quot;: false },
      { &quot;Id&quot;: 3, &quot;Name&quot;: &quot;Item3&quot;, &quot;IsComplete&quot;: false }
  ]
  </code></pre>
  <p>As you see the Item4 gets added to where the last deleted item was. After this if I add subsequent items it continues to add the items at the end of the list and returns so. This is probably because of EF's InMemory Database Provider I am guessing, as it is primarily for test purposes. But even though, I wanted to know how the list works behind this phenomenon.</p>
  <p>Using the local watch window didn't help, because it did actually delete the Item2 object from the list and apparently shifted the list to add the new item.</p>
  <p><strong>Edit: When I am talking about how list works here, what I mean is how DbSet works in this add and remove operations as _context.TodoItems is a DbSet object and the ToList() is just converting that to a list.</strong></p>