Skip to content

Fully support complex types with the InMemory provider #31464

Description

@roji

Some query/materialization support is missing, see tests DbContextTest.SaveChanges_calls_DetectChanges_by_default, DbContextTest.Auto_DetectChanges_for_SaveChanges_can_be_switched_off

Activity

  1. added this to the Backlog milestone on Sep 6, 2023
  2. ajcvickers commented on Sep 6, 2023

    @ajcvickers
    Contributor

    Note from triage: we are very unlikely to implement this. See #18457 (comment)

  3. aliastopan commented on Jan 31, 2024

    @aliastopan

    Is the KeyNotFoundException part of the reason why ComplexProperty has yet compatible with in-memory database?

    "title": "KeyNotFoundException", "code": 500, "source": "System.Private.CoreLib", "detail": "The given key 'Property: UserAccount.User#User.EmailAddress (string) Required MaxLength(255)' was not present in the dictionary.", "stackTrace": [ " at TValue System.Collections.Generic.Dictionary<TKey, TValue>.get_Item(TKey key)", " at Expression Microsoft.EntityFrameworkCore.InMemory.Query.Internal.InMemoryShapedQueryCompilingExpressionVisitor+ShaperExpressionProcessingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)",

  4. roji commented on Jan 31, 2024

    @roji
    MemberAuthor

    @aliastopan support hasn't been done for InMemory, this specific KeyNotFoundException may be part of that (or not). We're unlikely to implement support, since using the InMemory database for testing is discouraged - consider testing against your containerized database (e.g. SQL Server) using testcontainers.net.

  5. IanWitham commented on Sep 20, 2024

    @IanWitham

    Is this documented somewhere? I've just sunk hours into trying to "solve" this before I came across this thread.

  6. roji commented on Sep 20, 2024

    @roji
    MemberAuthor

    @IanWitham yes, the InMemory provider documentation contains the following note:

    New features are not being added to the in-memory database.

    Aside from the numerous other warnings and notices to stay away from the provider in general.

  7. marchy commented on Sep 21, 2024

    @marchy

    @roji the right guidance to give on that page would be to suggest people to use the SQLite with in-memory option set.

    There are definitely gaps there as well, but they are more or less workable and has been a godsend to enabling us to work in a rapid development model for new features (ie: not having to worry about migrating a live DB until you have the full domain model for that feature built out)

  8. roji commented on Sep 21, 2024

    @roji
    MemberAuthor

    the right guidance to give on that page would be to suggest people to use the SQLite with in-memory option set.

    We generally recommend against using database fakes, either InMemory or Sqlite - though Sqlite is indeed a much better option. In today's world, setting up SQL Server or PG via testcontainers and testing against that is both easy and very fast, and so testing against a fake is much less needed than it used to be.

    not having to worry about migrating a live DB until you have the full domain model for that feature built out

    You can just use EnsureCreated during rapid development, including with a real database - no need to deal with migrations.

  9. marchy commented on Sep 21, 2024

    @marchy

    @roji What about tests?
    We run all tests on Sqlite DB's that we create rapidly for each test instance (ie: in test creation/tear-down)

    Can this run disconnected from the Internet on local and test servers?

    It looks like a LOT more setup to pull off – but if that is indeed the guidance there should be a full guide for how to set it up in both local environment and Azure DevOps at the least.

  10. roji commented on Sep 21, 2024

    @roji
    MemberAuthor

    @marchy tests is what we're discussing here above (at least what I was discussing).

    Can this run disconnected from the Internet on local and test servers?

    Of course, the major databases can run in a docker container, which is very easy to manage. As I wrote above, I suggest taking a look at testcontainers, and also reading the EF testing docs which discuss a lot of this stuff.

  11. jukkahyv commented on Oct 10, 2024

    @jukkahyv

    Of course, the major databases can run in a docker container, which is very easy to manage. As I wrote above, I suggest taking a look at testcontainers, and also reading the EF testing docs which discuss a lot of this stuff.

    Can you give a practical example of how it would work in the ideal situation? I mean some medium-large open source website that makes use of testcontainers to efficiently run 100s of tests. Because the problem is, I've only seen bad examples. Projects that try to do it that way, then 2-3 years afterwards it takes 20 minutes to run the tests, tests randomly fail because of database timeouts, and test data management is a nightmare because it's being reused between tests. I'm not against using real database for tests, I just haven't seen it working.

  12. roji commented on Oct 10, 2024

    @roji
    MemberAuthor

    @jukkahyv please take a thorough look at the testing docs I linked to above; these are also related to these code samples which should be helpful. I do think we should improve these docs to specifically show testing with testcontainers (they were written before that was a thing), and be more practically-oriented, but the general idea is there.

    EF itself runs almost all of its test against real databases - that's a huge number of tests that run against SQL Server alone, in a very reasonable amount of time. It's a matter of structuring test databases appropriately, not e.g. creating one for every test, and running everything in parallel while ensuring that modification tests e.g. use transactions which get rolled back, so as to not cause interference with other tests.

  13. dta5003 commented on Nov 19, 2024

    @dta5003

    It's totally understandable that no new features are being added to the InMemory provider.

    If possible, a better error message/exception would be super helpful when attempting to interact with Complex Types via the InMemory provider. As far as I can tell after many hours yesterday, finding this GitHub issue via searching is the only way to discover the root cause of this "KeyNotFoundException".

  14. added theissue type on Jun 15, 2025
  15. erikatsg commented on Feb 27, 2026

    @erikatsg

    Is the EFCore position (no development of inmemorydatabase) still true about this?
    I saw now that your AI agent might have fixed this issue in 10.x-

    Wondering since I have this issue in dotnet 9 and thinking about if I should be moving over to testcontainers or postpone test development until we have moved to dotnet 10 where this might be fixed for in-memory DB.

  16. roji commented on Feb 27, 2026

    @roji
    MemberAuthor

    Yes - we're not investing in InMemory. You may see a fix here and there, but we're still not actively developing it or adding new features.

  17. erikatsg commented on Feb 27, 2026

    @erikatsg

    @roji thank you for clarifying this again. I will then consider testcontainers instead.

  18. masesdevelopers commented on Mar 5, 2026

    @masesdevelopers

    Hi @roji, we developed a provider based on Apache Kafka as backend. At the beginning, reading the online documentation, MS wrote that the best way to build a new provider is to start from an existing one. Since in our provider each Kafka topic is materialized like a table we identified the InMemory provider as the most closest to our needs. Introducing the management of complex type for specific needs related to the ability to mix EF Core with the rich OPC-UA type system, a similar KeyNotFoundException was raised and we come to this open issue searching for some reasons. Just to conclude, if it can be interesting for you: the solution seems very simple and there are only 4 points to be touched (masesgroup/KEFCore#477). We tested just one complex type and one owned type, more complex tests will be done using OPC-UA type system.
    Maybe you have to just improve the work started with #36366.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions