Using MariaDB in an ASP.NET Core API with Entity Framework Core

Using MariaDB in an ASP.NET Core API with Entity Framework Core

MariaDB Server is one of the most popular open source relational databases. It’s made by the original developers of MySQL and guaranteed to stay open source. It is part of most cloud offerings and the default in most Linux distributions.

In this post, we will see how to use MariaDB as storage for an ASP.NET API and do simple CRUD operations with Entity Framework Core.

Installing MariaDB

Visit the https://downloads.mariadb.org/ and download the latest stable version. At the moment of writing this article the last stable version is 10.5.4, so I would suggest to download the MSI packaged named “mariadb-10.5.4-winx64.msi” for easier installation.

You can also download the installation package here: mariadb-10.5.4-winx64.msi.

Installing an MSI package is rather easy, so I am not going to get into details about it. When you’ re done, a shortcut of the management tool that comes with MariaDB will be placed on your desktop; it is called HeidiSQL. It doesn’t feature the best UI around, but you can do all sort of operations with it!

If you wish to go into the MySQL world, I would suggest DBeaver Community Edition as management tool, which you can download here: https://dbeaver.io/download/

Creating the schema

We can use HeidiSQL for this! Double click on the HeidiSQL shortcut, fill in the correct username & password (the ones you gave during installation) and click “Open“. Once done, right click on the server’s entry in the tree on the left (= the topmost one). In its context menu, you have “Create new > Database” that you can use to -obviously- create a new database! Follows an image that makes things a bit clearer!
MariaDB - Create New Database

MySQL case sensitivity depends on the OS, so I would suggest to always use lower case in database and table names.

It is convenient that LiteDB (and MongoDB) is creating collection on demand, but here we need to create the table ourselves. We are going to use Microsoft’s example of a weather forecast API as a base for this post, so the only table needed is a table to store the WeatherForcast object. Just had back to HeidiSQL, and run the following script:

Setting up the API.NET Core API project

We need to create a new webapi to use Microsoft’s example and this is rather easy; the only addition is the Pomelo.EntityFrameworkCore.MySql nuget package. Assuming thus basic knowledge on how to create a new ASP.NET Core API, I will just list the necessary commands and continue with the interesting stuff:

You could also choose to use Visual Studio to create a new ASP.NET Core Web Application, choose “API” as the project template and finally add the Pomelo.EntityFrameworkCore.MySql nuget package.

That’s it. ASP.NET Core rules right? Last but not least, we should keep somewhere the connection string for the database, and there is no better place for it than the appsettings. Add the following lines to the appsettings.Development.json:

Setting up the DbContext

Unlike the setup with LiteDB in the article “Using LiteDB in an ASP.NET Core API” we will not have one connection to share with all requests; Instead, we will just use AddDbContextPool to inject the DbContext as scoped in our service.

First of all, let’s create our DataModel, which in reality just adds the public int? Id { get; set; } line in the model of the official example:

The Id of this model is indeed the primary key and is set as auto-increment in the database, but it is nullable in the model because inserts should not define an Id. This of course is not a proper solution for a real life app, but it is here to serve as a sample.

In a real life application we should create different models for the database and for all requests, but now, for the sake of simplicity, we are going to use the WeatherForecast object all over.

After we are done with the model, we should create a custom DbContext (let’s name it MariaDbContext) that inherits from Microsoft.EntityFrameworkCore.DbContext and lists our one and only WeatherForecastDataModel:

Finally, let’s add the MariaDbContext as a scoped service to the DI Container:

Setting up a service for MariaDB

We should create a service that stores and retrieves the model to/from the database. Since this is done with Entity Framework Core, it is done the way we already know. This is a simplified version of a service with full CRUD operations:

And again add it as a service to the IoC container:

Finally, setting up the controller

Having done all the hard work, it is now time to connect our service with the WeatherForecastController. The cotroller included as a sample contains just a hard-coded GET action, so we need to rewrite all of it and support the operations we wrote in the IMariaDbWeatherForecastService:

The end!

MariaDB is a community-developed, commercially supported fork of the MySQL relational database management system (RDBMS), intended to remain free and open-source software under the GNU General Public License. Development is led by some of the original developers of MySQL, who forked it due to concerns over its acquisition by Oracle Corporation in 2009.

Why choose it for your RDBMS instead of SQL Server? Well it’s free and it’s in Azure already!

My playground with MariaDB, including postman calls and the complete API, you can find in https://github.com/georgekosmidis/AspNetCore.MariaDb!