diff options
Reworked connect to mysql tutorial and renamed it
5 files changed, 87 insertions, 96 deletions
| -rw-r--r-- | src/tutorials/connect-to-mysql-in-csharp-ef/article.md | 96 | ||||
| -rw-r--r-- | src/tutorials/connect-to-mysql-in-dotnet-ef/article.md | 87 | ||||
| -rw-r--r-- | src/tutorials/connect-to-mysql-in-dotnet-ef/img/about-workbench.png | bin | 0 -> 24274 bytes | |||
| -rw-r--r-- | src/tutorials/connect-to-mysql-in-dotnet-ef/img/connections.png (renamed from src/tutorials/connect-to-mysql-in-csharp-ef/img/connections.png) | bin | 10067 -> 10067 bytes | |||
| -rw-r--r-- | src/tutorials/connect-to-mysql-in-dotnet-ef/img/workbench-version.png | bin | 0 -> 33595 bytes |
diff --git a/src/tutorials/connect-to-mysql-in-csharp-ef/article.md b/src/tutorials/connect-to-mysql-in-csharp-ef/article.md deleted file mode 100644 index a028ae8..0000000 --- a/src/tutorials/connect-to-mysql-in-csharp-ef/article.md +++ /dev/null @@ -1,96 +0,0 @@ -# Connect to MySql in C# and Entity Framework - -**Article Created: 21 May 2020** || **Last Edited: 25 November 2020** - -## [*Due for rework*](/rework.html) - ---- - -In my experience, connecting a `C#` application to a `MySQL` database with Entity Framework can be quite tricky. Here, I share what works for me. - -## Contents -<div></div> - -1. [Preparation](#Preparation) - 1. [MySql](#MySql) - 2. [NuGet packages](#NuGet%20packages) -2. [Connecting](#Connecting) - 1. [Connection string](#Connection%20string) - 2. [ASP.NET Startup](#ASP.NET%20Startup) - 3. [DBContext child class](#DBContext%20child%20class) - -## Preparation -[](#Preparation) - -<span></span> - -### MySql -[](#MySql) - -This tutorial doesn't cover it, but you'll need to have properly [installed and configured](https://dev.mysql.com/doc/mysql-installation-excerpt/5.7/en/) `MySQL`. - -### NuGet packages -[](#NuGet%20packages) - -You'll need to install two NuGet packages: [Microsoft.EntityFrameworkCore](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore) and [MySql.Data.EntityFrameworkCore](https://www.nuget.org/packages/MySql.Data.EntityFrameworkCore/). - -[Microsoft.EntityFrameworkCore](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore) is the [object-relational mapping](https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping) implementation we'll be using. It doesn't offer the specifics for connecting to a `MySQL` database, but it does everything else. - -[MySql.Data.EntityFrameworkCore](https://www.nuget.org/packages/MySql.Data.EntityFrameworkCore/) is what enables Microsoft.EntityFrameworkCore to connect to a `MySQL` database. - -## Connecting -[](#Connecting) - -<span></span> - -### Connection string -[](#Connection%20string) - -You can find a truckload of ways to make your connection string from places like [this](https://www.connectionstrings.com/mysql/) but in this case, I've found out that most don't work. -After a lot of trial and error, I've found that this one works the best: - -``` -"server=localhost; port=YOUR-PORT; user id=YOUR-USER-ID; password=YOUR-PASSWORD; database=NAME-OF-DATABASE; SslMode=none" -``` - -If you are using `MySQL Workbench` and you don't know your port and id, you can look at the server information from the `MySQL Connections` page. - - In this example, the user id is _root_ and the port is _3306_. - -### ASP.NET Startup -[](#ASP.NET%20Startup) - -If you are making an `ASP.NET` application, you'll need to put the connection string in the `ConfigureServices` method of the `Startup.cs` file. - -You'll also need to modify your `ConfigureServices` method, by replacing `.UseSQL` with `.UseMySQL`. - -The result should look something like this: - -``` -// This method gets called by the runtime. Use this method to add services to the container. -public void ConfigureServices(IServiceCollection services) -{ - services.AddControllersWithViews(); - services.AddTransient<UserServices, UserServices>(); - services.AddDbContext<AppDbContext>(opt => - { - opt.UseMySQL(CONNECTION_STRING); - }); -} -``` - -### DBContext child class -[](#DBContext%20child%20class) - -If you want to connect a `DBContext` child class to a `MySQL` database, one of the easiest ways to do it, is to override the `OnConfiguring` method. - -In the override, you'll need to call the `optionsBuilder.UseMySQL` method (`optionsBuilder` is a parameter of the method) with the connection string. - -``` -protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) -{ - optionsBuilder.UseMySQL(CONNECTION_STRING); -} -``` - -Et voilà ! You should now be connected. diff --git a/src/tutorials/connect-to-mysql-in-dotnet-ef/article.md b/src/tutorials/connect-to-mysql-in-dotnet-ef/article.md new file mode 100644 index 0000000..4380e3b --- /dev/null +++ b/src/tutorials/connect-to-mysql-in-dotnet-ef/article.md @@ -0,0 +1,87 @@ +# Connect to MySql in .NET and Entity Framework + +**Article Created: 21 May 2020** || **Last Edited: 19 June 2021** + +--- + +I have found that connecting a `.NET` application to a `MySql` database (with Entity Framework) can be tricky. Here is what works for me. + +Also, [MySql.Data.EntityFrameworkCore](https://www.nuget.org/packages/MySql.Data.EntityFrameworkCore/) has been **depricated** since *October 2020*. If you need setup instructions with it, read [this](https://gitlab.com/Syndamia/syndamiadotcom/-/blob/0510e29526c6d7681587fff4c98a339e8c69e9b5/src/tutorials/connect-to-mysql-in-csharp-ef/article.md). + +## Contents +<div></div> + +1. [NuGet packages](#NuGet%20packages) +2. [Connection string](#Connection%20string) +3. [Connecting](#Connecting) + 1. [ASP.NET Startup](#ASP.NET%20Startup) + 2. [DBContext child class](#DBContext%20child%20class) + +## NuGet packages +[](#NuGet%20packages) + +You'll need to install [Pomelo.EntityFrameworkCore.MySql](https://www.nuget.org/packages/Pomelo.EntityFrameworkCore.MySql/). It provides the stuff necessary for connecting and using a `MySql` database. + +## Connection string +[](#Connection%20string) + +There are [a lot](https://www.connectionstrings.com/mysql/) of ways to setup the connection string but I've found that this one works the best: + +``` +"server=localhost; port=YOUR-PORT; user id=YOUR-USER-ID; password=YOUR-PASSWORD; database=NAME-OF-DATABASE; SslMode=none" +``` + +If you are using `MySQL Workbench` and you don't know your port and id, you can look at the server information from the `MySQL Connections` page. + + In this example, the user id is _root_ and the port is _3306_. + +## Connecting +[](#Connecting) + +To connect, you'll need to call the `UseMySql` method, either in [ASP.NET Startup](#ASP.NET%20Startup) or in a [DBContext child class](#DBContext%20child%20class). + +The method requires at least two arguments: connection string and `MySql` server version. The latter you can find, by going to the `Help/About Workbench` window inside `MySQL Workbench`. + + + + +For the server version argument, create a new object of type `MySqlServerVersion` and give it's constructor a new object of type `Version`, to which you give the version numbers. The result should look something like this: + +``` +.UseMySql(CONNECTION_STRING, new MySqlServerVersion(new System.Version(8, 0, 21))) +``` + +Where `CONNECTION_STRING` is a (constant) variable that contains your connection string, and 8, 0, 21 are the version numbers. + +### ASP.NET Startup +[](#ASP.NET%20Startup) + +For `ASP.NET` applications, the connection must be put inside the `ConfigureServices` method of the `Startup.cs` file. If you already have a `.UseSQL` method call in there, just replace it with your `.UseMySql` method and arguments. + +The result should look something along the lines of: + +``` +// This method gets called by the runtime. Use this method to add services to the container. +public void ConfigureServices(IServiceCollection services) +{ + ... + services.AddDbContext<AppDbContext>(opt => + { + opt.UseMySql(...); + }); +} +``` +<p></p> + +### DBContext child class +[](#DBContext%20child%20class) + +Another way to connect is via a `DBContext` child class (so, inside your own DB context). Just, override the `OnConfiguring` method and call the `.UseMySql` method from the `optionsBuilder` parameter. + +``` +protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) +{ + optionsBuilder.UseMySql(...); +} +``` +<p></p> diff --git a/src/tutorials/connect-to-mysql-in-dotnet-ef/img/about-workbench.png b/src/tutorials/connect-to-mysql-in-dotnet-ef/img/about-workbench.png Binary files differnew file mode 100644 index 0000000..067d8ce --- /dev/null +++ b/src/tutorials/connect-to-mysql-in-dotnet-ef/img/about-workbench.png diff --git a/src/tutorials/connect-to-mysql-in-csharp-ef/img/connections.png b/src/tutorials/connect-to-mysql-in-dotnet-ef/img/connections.png Binary files differindex aa7b408..aa7b408 100644 --- a/src/tutorials/connect-to-mysql-in-csharp-ef/img/connections.png +++ b/src/tutorials/connect-to-mysql-in-dotnet-ef/img/connections.png diff --git a/src/tutorials/connect-to-mysql-in-dotnet-ef/img/workbench-version.png b/src/tutorials/connect-to-mysql-in-dotnet-ef/img/workbench-version.png Binary files differnew file mode 100644 index 0000000..bf5081d --- /dev/null +++ b/src/tutorials/connect-to-mysql-in-dotnet-ef/img/workbench-version.png |