Skip to content

InMemory provider queries on tracked entities (unlike SQLServer provider) #12945

Closed

Description

@wizofaus

With the InMemory provider, if you modify the value of an entity returned by EF, then immediately query on it, the query successfully finds the entity.
However when using SQLServer it doesn't, as the change hasn't been committed to the database yet.

E.g.

 class MyContext : DbContext
 {
       internal DbSet<MyObject> Objects { get; set; }

       void Foo()
       {
              Objects.FirstOrDefault().Value = 42;
              Console.WriteLine($"Found: {Objects.FirstOrDefault(o => o.Value == 42) != null}");
       }
 }

Assuming your database contains at least one Object with a value field not equal to 42, then using InMemory, this prints out "Found: true", but using SQLServer, prints out "Found: false".

Adding "SaveChanges();" before performing the query causes the behaviour to be the same, but as it is this discrepancy hid a rather nasty bug in my server that didn't show up in any of the automated testing.

Activity

  1. wizofaus commented on Aug 9, 2018

    @wizofaus
    Author

    There's at least 3 different ways in which In-Memory provider works differently to the SQL-server provider. I'm happy to contribute a fix if there's nobody else working on it.

  2. ajcvickers commented on Aug 9, 2018

    @ajcvickers
    Contributor

    Note for triage: I was able to reproduce the case above. It looks like the in-memory query provider is doing identity resolution to the tracked entity before evaluating the predicate.

    @david-waterworth @wizofaus The in-memory database will never behave exactly like SQL Server, or indeed any other relational database. However, we are tracking some issues to add additional functionality--for example, #6872

  3. wizofaus commented on Aug 9, 2018

    @wizofaus
    Author

    Sure, I'd never expect it to behave exactly the same, and indeed previous In-Memory DbSet libraries for EF 6 (.net Framework) had the same problem (or even worse - no distinction at all between tracked entity data and persisted data). But the fact that "real" databases query against the underlying data rather than current tracked entities would seem to be a useful behaviour to emulate.

  4. changed the title [-]InMemory provider doesn't work the same way SQLServer[/-] [+]InMemory provider doesn't work the same as SQLServer[/+] on Aug 9, 2018
  5. changed the title [-]InMemory provider doesn't work the same as SQLServer[/-] [+]InMemory provider queries on tracked entities (unlike SQLServer provider)[/+] on Aug 9, 2018
  6. ajcvickers commented on Aug 9, 2018

    @ajcvickers
    Contributor

    @wizofaus The case you reported above is a bug. In-memory queries are made against the in-memory database, not the tracked entity instances.

  7. added this to the 3.0.0 milestone on Aug 10, 2018
  8. removed this from the 3.0.0 milestone on Aug 13, 2018
  9. added this to the 3.0.0 milestone on Aug 15, 2018
  10. 5 remaining items

  11. removed their assignment
    on Aug 7, 2019
  12. smitpatel commented on Dec 7, 2019

    @smitpatel
    Contributor

    InMemory is working same as SqlServer in 3.1 release. We don't track against tracked entities anymore.

  13. modified the milestones: Backlog, 3.1.0 on Dec 7, 2019
  14. self-assigned this
    on Dec 7, 2019
  15. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions