I want to use all open-source software so it’s going to be using a MySQL database and be hosted on a Linux box somewhere. During these months all the coffees (and more) will be used to support the Ukraine.I’ve written an. You can support my blog by buying a coffee. Here you’ve seen using MySQL and probably also learned using the Docker container for MySQL. In my book I’m using both Microsoft SQL Server as well as Azure Cosmos DB. Take awayĮF Core gives us a provider-based approach to database access. The first sample application works without any change (other than changing the provider), and I expect most of the other samples from the book to work without big issues. Of course there are different conventions between different providers, and not all the features might behave in the same way. The application runs using the MySQL database. Creating a database, adding, querying, updating, and deleting records – no change is needed. To use MySQL, this API invocation needs to be changed to UseMySql:Īnd this is all what’s needed to run the sample application. The original code of the sample application was using the API UseSqlServer to access Micrsoft SQL Server. The sample application makes use of dependency injection to inject the DbContext into the controller. ConnectionStrings:MySQLConnection=server=localhost port=3306 database=mysqlsample user=root password=UseASecretPassword Running the sample application, during development the connection string is retrieved from the applications’s user secrets to avoid exposing the password in the repository. Check a link below for a list of EF Core database providers. For MySQL, providers are also available from Oracle, and DevArt. Andriy Svyryd, a developer on the EF Core team, is also a contributor to this open source repository. The Pomelo Foundation offers EF Core providers for MySQL and MariaDB. All what needs to be done with the Intro sample is to remove the provider for SQL Server, and replace it by a provider for MySQL. To use a sample application, I’m using one of the many EF Core code samples from my book. Check the link MySQL on Windows using Docker for information how this can be done. A recommended practice is not to use the root user, and create a different user with more restrictive permissions. With the sample application I’m just using the root user to create and access a database. With the sample application, I’m creating a database from the application’s code. The -d option is used to detach the console to run the container in the background.įor accessing the database directly within the Docker container, docker exec can be used, passing the name of the container, the username ( -u) and the password ( -p) as arguments: docker exec -it mysql1 mysql -uroot -pUseASecretPasswordĭocker exec requires the name of the container ( mysql1), as well as the command that should be invoked ( mysql). I’m also using the environment variables MYSQL_ROOT_HOST and MYSQL_ROOT_PASSWORD to allow all IP addresses from the host to access the database, and to set the root password. With the option -p I’m telling Docker to expose the internal port 3306 outside on the host machine with the same port number 3306. The Docker container needs to be accessible from the outside. To run a Docker container with MySQL I’m using the following command: docker run -name mysql1 -p 3306:3306 -e MYSQL_ROOT_HOST=% -e MYSQL_ROOT_PASSWORD=UseASecretPassword -d mysql:latest On my system I’m running Docker Desktop with WSL-2. For not needing to install MySQL on my system, I’m using a Docker image as you can read here: To demonstrate using EF Core I took one of the existing samples and changed the SQL Server provider to a MySQL provider. When using EF Core I’m using Micorosft SQL Server or Azure Cosmos DB – and you have many code samples for EF Core in my book Professional C# and. Recently I’ve been asked for a sample using EF Core with MySQL.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |