Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

ASP.NET Core 6

How to use EF Core as an in-memory database in ASP.NET Core 6

In this article, we’ll learn how to use EF Core as an in-memory database in ASP.NET Core 6. We’ll create an ASP.NET Core Web API project in Visual Studio 2022, and then we’ll install the EF Core InMemory NuGet package. We’ll create a custom DbContext class, and then we’ll create model classes. We’ll create a Repository class, and then we’ll add the Repository service to the services container. Finally, we’ll create a Web API controller.

What is EF Core? When should we use it?

EF Core is a lightweight, extensible, open source and cross-platform version of the Entity Framework data access technology. EF Core can be used with various .NET platforms, such as .NET Framework, .NET Core, Mono, and Xamarin. EF Core is an object-relational mapper (O/RM) that enables .NET developers to work with a database using .NET objects. It eliminates the need for most of the data-access code that developers usually need to write.

What is an in-memory database?

An in-memory database is a database that runs in memory instead of on disk. In-memory databases are faster than disk-based databases because they can avoid the latency of disk access. In-memory databases are often used for unit testing, because they can provide a consistent environment for tests.

Using an in-memory database in ASP.NET Core 6

In ASP.NET Core 6, we can use an in-memory database for unit testing. To use an in-memory database, we need to install the EF Core InMemory NuGet package. We can then create a custom DbContext class, and create model classes. We can create a Repository class, and add the Repository service to the services container. Finally, we can create a Web API controller.

Install the EF Core InMemory NuGet package

To install the EF Core InMemory NuGet package, we can use the following command:

dotnet add package Microsoft.EntityFrameworkCore.InMemory

Create a custom DbContext class in ASP.NET Core 6

To create a custom DbContext class, we can use the following code:

public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions options)
: base(options)
{ }

public DbSet Blogs { get; set; }
}

Create model classes in ASP.NET Core 6

To create model classes, we can use the following code:

public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
}

Create a Repository class in ASP.NET Core 6

To create a Repository class, we can use the following code:

public class BlogRepository
{
private readonly MyDbContext _context;

public BlogRepository(MyDbContext context)
{
_context = context;
}

public IEnumerable GetAll()
{
return _context.Blogs.ToList();
}

public Blog GetById(int id)
{
return _context.Blogs.FirstOrDefault(b => b.BlogId == id);
}

public void Add(Blog blog)
{
_context.Blogs.Add(blog);
_context.SaveChanges();
}

public void Update(Blog blog)
{
_context.Blogs.Update(blog);
_context.SaveChanges();
}

public void Delete(Blog blog)
{
_context.Blogs.Remove(blog);
_context.SaveChanges();
}
}

Add the Repository service to the services container in ASP.NET Core 6

To add the Repository service to the services container, we can use the following code:

public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext(options =>
options.UseInMemoryDatabase(“MyDatabase”));
services.AddScoped();
}

Create a Web API controller in ASP.NET Core 6

After we’ve added the Repository service to the services container, we can create a Web API controller. The Web API controller is responsible for handling HTTP requests and responses.

To create a Web API controller, we can use the following code:

[Route(“api/[controller]”)]
public class BlogsController : Controller
{
private readonly BlogRepository _repository;

public BlogsController(BlogRepository repository)
{
_repository = repository;
}

[HttpGet]
public IEnumerable<Blog> Get()
{
return _repository.GetAll();
}

[HttpGet(“{id}”)]
public Blog Get(int id)
{
return _repository.GetById(id);
}

[HttpPost]
public void Post([FromBody]Blog blog)
{
_repository.Add(blog);
}

[HttpPut(“{id}”)]
public void Put(int id, [FromBody]Blog blog)
{
blog.BlogId = id;
_repository.Update(blog);
}

[HttpDelete(“{id}”)]
public void Delete(int id)
{
var blog = _repository.GetById(id);
_repository.Delete(blog);
}

}

Conclusion

In this article, we’ve learned how to use EF Core as an in-memory database in ASP.NET Core 6. We’ve installed the EF Core InMemory NuGet package, and then we’ve created a custom DbContext class. We’ve created model classes, and then we’ve created a Repository class. We’ve added the Repository service to the services container, and then we’ve created a Web API controller.