Wednesday, October 5, 2022
HomeC#Utilizing MySQL in a Docker container with EF Core – csharp.christiannagel.com

Utilizing MySQL in a Docker container with EF Core – csharp.christiannagel.com


Not too long ago I’ve been requested for a pattern utilizing EF Core with MySQL. When utilizing EF Core I’m utilizing Micorosft SQL Server or Azure Cosmos DB – and you’ve got many code samples for EF Core in my e book Skilled C# and .NET, 2021 Version. To show utilizing EF Core I took one of many current samples and adjusted the SQL Server supplier to a MySQL supplier. For not needing to put in MySQL on my system, I’m utilizing a Docker picture as you may learn right here:

  • Operating a Docker container with MySQL
  • Utilizing the MySQL supplier with EF Core

MySQL

Operating a Docker container with MySQL

On my system I’m working Docker Desktop with WSL-2. To run a Docker container with MySQL I’m utilizing the next command:

docker run --name mysql1 -p 3306:3306 -e MYSQL_ROOT_HOST=% -e MYSQL_ROOT_PASSWORD=UseASecretPassword -d mysql:newest

The Docker container must be accessible from the surface. With the choice -p I’m telling Docker to show the inner port 3306 outdoors on the host machine with the identical port quantity 3306. I’m additionally utilizing the setting variables MYSQL_ROOT_HOST and MYSQL_ROOT_PASSWORD to permit all IP addresses from the host to entry the database, and to set the basis password. The -d choice is used to detach the console to run the container within the background.

For accessing the database straight inside the Docker container, docker exec can be utilized, passing the identify of the container, the username (-u) and the password (-p) as arguments:

docker exec -it mysql1 mysql -uroot -pUseASecretPassword

docker exec requires the identify of the container (mysql1), in addition to the command that ought to be invoked (mysql).

With the pattern utility, I’m making a database from the applying’s code. See a hyperlink under for MySQL instructions. With the pattern utility I’m simply utilizing the basis consumer to create and entry a database. A really useful follow is to not use the basis consumer, and create a special consumer with extra restrictive permissions. Test the hyperlink MySQL on Home windows utilizing Docker for data how this may be completed.

EF Core with MySQL

To make use of a pattern utility, I’m utilizing one of many many EF Core code samples from my e book. All what must be completed with the Intro pattern is to take away the supplier for SQL Server, and change it by a supplier for MySQL. With the pattern utility I’m utilizing Pomelo.EntityFrameworkCore.MySQL. The Pomelo Basis gives EF Core suppliers for MySQL and MariaDB. Andriy Svyryd, a developer on the EF Core crew, can also be a contributor to this open supply repository. For MySQL, suppliers are additionally accessible from Oracle, and DevArt. Test a hyperlink under for a listing of EF Core database suppliers.

Operating the pattern utility, throughout improvement the connection string is retrieved from the purposes’s consumer secrets and techniques to keep away from exposing the password within the repository.

ConnectionStrings:MySQLConnection=server=localhost;port=3306;database=mysqlsample;consumer=root;password=UseASecretPassword

The pattern utility makes use of dependency injection to inject the DbContext into the controller. The unique code of the pattern utility was utilizing the API UseSqlServer to entry Micrsoft SQL Server. To make use of MySQL, this API invocation must be modified to UseMySql:

Using MySQL provider

And that is all what’s wanted to run the pattern utility. Making a database, including, querying, updating, and deleting information – no change is required. The appliance runs utilizing the MySQL database.

After all there are totally different conventions between totally different suppliers, and never all of the options would possibly behave in the identical means. The primary pattern utility works with none change (aside from altering the supplier), and I count on many of the different samples from the e book to work with out huge points.

Take away

EF Core provides us a provider-based strategy to database entry. In my e book I’m utilizing each Microsoft SQL Server in addition to Azure Cosmos DB. Right here you’ve seen utilizing MySQL and possibly additionally realized utilizing the Docker container for MySQL.

Take pleasure in studying and programming!

Christian

You may help my weblog by shopping for a espresso. Throughout these months all of the coffees (and extra) might be used to help the Ukraine.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments