Creating a mock database for EF Core with Bogus
Link - https://www.amazon.com/ASP-NET-Core-Web-API-Cookbook/dp/1835880347/ We will use SQLite’s in-memory database provider with EF Core, eliminating the need for database files or server connections. The API’s database will be populated with mock data generated by Bogus. Getting ready To begin, you will need the following: The .NET 9 SDK, which can be downloaded from https://dotnet.microsoft.com/en-us/download/dotnet/9.0 How to do it… Open the terminal and create a new web API: Navigate to the project directory and create a new .gitignore file: Install EF Core and its SQLite provider: Install Bogus for creating mock data. New in .NET 9, we also have to manually add Swagger support: Create a folder named Models. Create a file called Product.cs and fill in the following Product class: Create a sibling folder called Data. In that folder, create your AppDbContext.cs file. Fill in a AppDbContext class, which will inherit from DbContext: Still inside the AppDbContext class, on the next line, define the 'DbSet' property to use your new Products class: On the next line, define the OnModelCreating method: Open the Program.cs file. Delete all the boilerplate code that .NET generated; we are going to start from scratch. At the top of Program.cs, import our new Data namespace, as well as the namespace for models and Bogus itself: Next, we will create the builder, register the OpenAPI service, and create a connection to our in-memory database that will persist for the application’s lifetime: After creating the SQLite connection, we need to add the DbContext registration: On the next line, inject a scoped service that opens a connection to the in-memory database and then confirms that the connection has been created. Service lifetime selection We need to register a scoped service to interact with EF Core’s DbContext. This is counterintuitive; it might seem like a singleton service would be a more appropriate lifetime for working with a database. However, EF Core's DbContext is designed to be short-lived and is not thread-safe for concurrent operations. Expand the scoped service to seed your in-memory database with fake data, only if your database context is empty: Let’s add a minimal API endpoint we can use for testing: Before we run the application, let’s manually add SwaggerUI support: Run the application. You can visit http://localhost/swagger/index.html and click on the Products endpoint to see our fake data generated by Bogus: How it works... You registered your AppDbContext with the service provider at startup, which is the standard way to integrate EF Core into ASP.NET Core dependency injection. This allows the database context to be available for your controllers, services, and so on.You also added a scoped service provider that checks whether your database is empty. The scoped lifetime ensures that a new AppDbContext is created for each request, preventing any data inconsistencies that can plague singleton instances of database connections. If the database is empty, it will be seeded using the Faker class from Bogus.We also used the SQLite in-memory database provider for EF Core. This allows us to create a database entirely in memory without requiring an external SQLite file. While EF Core also includes an InMemorydatabase provider, it is considered a legacy option and is not recommended for testing. Unlike the InMemory provider, SQLite’s in-memory database supports transactions and raw SQL, making it a closer approximation of a real-world database.

Link - https://www.amazon.com/ASP-NET-Core-Web-API-Cookbook/dp/1835880347/
We will use SQLite’s in-memory database provider with EF Core, eliminating the need for database files or server connections. The API’s database will be populated with mock data generated by Bogus.
Getting ready
To begin, you will need the following:
- The .NET 9 SDK, which can be downloaded from https://dotnet.microsoft.com/en-us/download/dotnet/9.0
How to do it…
- Open the terminal and create a new web API:
- Navigate to the project directory and create a new .gitignore file:
- Install EF Core and its SQLite provider:
- Install Bogus for creating mock data. New in .NET 9, we also have to manually add Swagger support:
- Create a folder named Models. Create a file called Product.cs and fill in the following Product class:
- Create a sibling folder called Data. In that folder, create your AppDbContext.cs file. Fill in a AppDbContext class, which will inherit from DbContext:
- Still inside the AppDbContext class, on the next line, define the 'DbSet' property to use your new Products class:
- On the next line, define the OnModelCreating method:
Open the Program.cs file. Delete all the boilerplate code that .NET generated; we are going to start from scratch.
At the top of Program.cs, import our new Data namespace, as well as the namespace for models and Bogus itself:
- Next, we will create the builder, register the OpenAPI service, and create a connection to our in-memory database that will persist for the application’s lifetime:
- After creating the SQLite connection, we need to add the DbContext registration:
- On the next line, inject a scoped service that opens a connection to the in-memory database and then confirms that the connection has been created.
Service lifetime selection
We need to register a scoped service to interact with EF Core’s DbContext. This is counterintuitive; it might seem like a singleton service would be a more appropriate lifetime for working with a database. However, EF Core's DbContext is designed to be short-lived and is not thread-safe for concurrent operations.
- Expand the scoped service to seed your in-memory database with fake data, only if your database context is empty:
- Let’s add a minimal API endpoint we can use for testing:
- Before we run the application, let’s manually add SwaggerUI support:
- Run the application. You can visit http://localhost/swagger/index.html and click on the Products endpoint to see our fake data generated by Bogus:
How it works...
You registered your AppDbContext with the service provider at startup, which is the standard way to integrate EF Core into ASP.NET Core dependency injection. This allows the database context to be available for your controllers, services, and so on.You also added a scoped service provider that checks whether your database is empty. The scoped lifetime
ensures that a new AppDbContext is created for each request, preventing any data inconsistencies that can plague singleton instances of database connections. If the database is empty, it will be seeded using the Faker class from Bogus.We also used the SQLite in-memory database provider for EF Core. This allows us to create a database entirely in memory without requiring an external SQLite file. While EF Core also includes an InMemorydatabase provider, it is considered a legacy option and is not recommended for testing. Unlike the InMemory provider, SQLite’s in-memory database supports transactions and raw SQL, making it a closer approximation of a real-world database.