By Joydip Kanjilal, Columnist, InfoWorld |
There are many reasons why you might want to use an in-memory database when working on ASP.NET Core 6 web applications. Maybe you’re developing a new feature and you want to test it without affecting your production database. Or perhaps you want a quick way to prototype something without setting up a whole new database.
Entity Framework Core, or EF Core for short, simplifies data access in .NET Core applications and allows you to write code to perform CRUD (create, read, update, delete) operations without directly interacting with the underlying database provider. The EF Core In-Memory Database Provider allows us to use EF Core with an in-memory database for testing.
The EF Core In-Memory Database Provider lets us store and retrieve data to and from memory in .NET Core 6 applications. Just remember that this provider was designed for testing purposes only.
This article discusses how we can use EF Core with an in-memory database in an ASP.NET Core 6 application. To work with the code examples provided in this article, you should have Visual Studio 2022 installed in your system. If you don’t already have a copy, you can download Visual Studio 2022 here.
First off, let’s create an ASP.NET Core project in Visual Studio 2022. Following these steps will create a new ASP.NET Core 6 Web API project in Visual Studio 2022:
We’ll use this ASP.NET Core 6 Web API project to work with EF Core and an in-memory database in the subsequent sections of this article.
EF Core is a lightweight, open-source, extensible ORM (Object Relational Mapper) that supports several database providers including SQLite, MySQL, PostgreSQL, and Microsoft SQL Server. (You’ll find a complete list of EF Core providers here.) And, as we’ve noted, EF Core supports storing and retrieving data to and from memory using its In-Memory Database Provider.
EF Core also supports LINQ (Language Integrated Query), which makes it easier to write queries against the data in your database. This is because LINQ allows you to write queries directly in C# instead of SQL or some other query language.
The ability to store and retrieve data in memory using the EF Core In-Memory Database Provider is especially well-suited for testing apps or for building applications that need to store data temporarily. And because an in-memory database is both fast and fast to set up, it is very handy to use one for unit testing.
An in-memory database is a database that resides in volatile memory instead of on a physical disk. Naturally, reads and writes for an in-memory database are many times faster than for a disk-based database, because the application need not wait for the data to be physically written to or read from the disk. Reading and writing data stored on a physical disk is a resource-intensive operation.
In-memory databases are often used for caching purposes, as they can hold a copy of often-used data in memory for quick access. You can also take advantage of in-memory databases to store transient data, i.e., data that does not need to be persisted to the disk.
There are also some notable downsides to using an in-memory database, though these apply to production use and not our testing scenario. One of these is that if you’re using an in-memory database, the data will not be persisted when the application stops running (unless you take steps to persist it). This means that if the application crashes, all of the data residing in the in-memory database will be lost.
Another downside is that an in-memory database uses much more memory compared to a traditional database that resides on a physical disk (and memory is far more expensive than disk storage). As a result, an in-memory database may not be suitable for applications that deal with vast amounts of data. But again, these downsides don’t really apply to using an in-memory database for testing.
In this section we’ll examine how we can use an in-memory database to store and retrieve data in an ASP.NET Core 6 application. We’ll follow these steps to create and use an in-memory database in ASP.NET Core 6:
Let’s get started!
To leverage the in-memory capabilities of EF Core in your application, you should add the Microsoft.EntityFrameworkCore.InMemory package to your project. To do this, select the project in the Solution Explorer window, then right-click and select “Manage NuGet Packages.” In the NuGet Package Manager window, search for the Microsoft.EntityFrameworkCore.InMemory package and install it.
Alternatively, you can install the package via the NuGet Package Manager console by entering the command shown below.
In EF Core, the DbContext is used by the application to interact with the underlying database. Before we proceed, let’s create a custom DbContext class that we’ll use to extend the DbContext class of the EF Core framework.
Note how we have specified the name of the in-memory database in the OnConfiguring method.
We’ll use two simple model classes for working with data in this application. These model classes are Author and Book. Create a new .cs file named Author.cs and enter the following code:
Create another .cs file named Book.cs and give it the following code:
Create an interface named IAuthorRepository in a file having the same name with a .cs extension and write the following code in there:
The AuthorRepository class implements the members of the IAuthorRepository interface as shown in the code listing given below.
Note how the custom DbContext we created earlier is used in the AuthorRepository class to store and retrieve data to and from the in-memory database.
To use dependency injection in your controllers or other classes in your project, you must add the services to the dependency injection container.
Add the following line of code in the Program.cs file to add the AuthorRepository service to the container.
Here is the complete source code of the Program.cs file for your reference:
So far so good. Now, right-click on the Controllers solution folder, click “Add -> Controller…”, and create a new API controller in your project. Name the controller AuthorController and replace the default generated code with the following:
Note how dependency injection has been used to inject an instance of type IAuthorRepository in the constructor of the AuthorController class. The HttpGet action method of the AuthorController class calls the GetAuthors method of the AuthorRepository class and returns a list of authors.
Finally, when you execute the application and hit the HttpGet endpoint of the AuthorController using Postman, you should see the output as shown in Figure 1 below.
Figure 1. Testing our simple in-memory database created with ASP.NET Core 6 and EF Core.
This was a minimalistic implementation of an ASP.NET Core 6 web application with the primary intent of showing you how to work with an in-memory database using EF Core. You should write your own custom code to generate the Ids for both of the model classes. Additionally, you might take advantage of unit tests to test the endpoints in your application. As you might expect, unit tests that leverage the EF Core In-Memory Database Provider will run quite fast.
Joydip Kanjilal is a Microsoft MVP in ASP.Net, as well as a speaker and author of several books and articles. He has more than 20 years of experience in IT including more than 16 years in Microsoft .Net and related technologies.
Copyright © 2022 IDG Communications, Inc.
Copyright © 2023 IDG Communications, Inc.