Search⌘ K
AI Features

Faking Database Interactions with Entity Framework Core

Explore how to fake database interactions in .NET using Entity Framework Core's in-memory databases. Understand how to set up a fake database context to run fast unit tests without real database connections, enabling realistic behavior and state management while keeping your original application code unchanged.

Entity Framework (EF) Core and other object-relational mappers (ORMs) available on .NET allow us to fake the database functionality using in-memory databases. This enables us to use the same code in our application as we would use for a real database. In contrast, we don’t have to use any real database connection in unit tests, which makes our tests fast.

In the context of automated testing, this technique is called faking. Unlike stubbing and mocking, faking allows us to interact with the object the same way we would interact with a real object, (i.e., the object exhibits a realistic behavior and returns realistic responses). The object can even keep its state, which certain interactions can modify.

A fake can be represented by an ...