From 0555703568964839dc5cff7871f88f36614f1d20 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 25 May 2021 21:14:35 +0300 Subject: Compacted Connect to MySQL in CSharp tutorial into one page --- .../connect-to-mysql-in-csharp-ef/article.md | 83 ++++++++++++++++++++++ .../connect-to-mysql-in-csharp-ef/connecting.md | 60 ---------------- .../connect-to-mysql-in-csharp-ef/introduction.md | 16 ----- .../connect-to-mysql-in-csharp-ef/preparation.md | 22 ------ 4 files changed, 83 insertions(+), 98 deletions(-) create mode 100644 src/tutorials/connect-to-mysql-in-csharp-ef/article.md delete mode 100644 src/tutorials/connect-to-mysql-in-csharp-ef/connecting.md delete mode 100644 src/tutorials/connect-to-mysql-in-csharp-ef/introduction.md delete mode 100644 src/tutorials/connect-to-mysql-in-csharp-ef/preparation.md (limited to 'src/tutorials') diff --git a/src/tutorials/connect-to-mysql-in-csharp-ef/article.md b/src/tutorials/connect-to-mysql-in-csharp-ef/article.md new file mode 100644 index 0000000..9067faa --- /dev/null +++ b/src/tutorials/connect-to-mysql-in-csharp-ef/article.md @@ -0,0 +1,83 @@ +# Connect to MySql in C# and Entity Framework + +**Article Created: 21 May 2020** || **Last Edited: 25 November 2020** + +--- + +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 +[![🔗](/img/link-tabler.svg)](#Contents) + +1. [Preparation](#Preparation)) +2. [Connecting](#Connecting) + +# Preparation + +## MySql +[![🔗](/img/link-tabler.svg)](#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 +[![🔗](/img/link-tabler.svg)](#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 + +## Connection string +[![🔗](/img/link-tabler.svg)](#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. + +![MyServer root 127.0.0.1:3306](./img/connections.png) In this example, the user id is _root_ and the port is _3306_. + +## ASP.NET Startup +[![🔗](/img/link-tabler.svg)](#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(); + services.AddDbContext(opt => + { + opt.UseMySQL(CONNECTION_STRING); + }); +} +``` + +## DBContext child class +[![🔗](/img/link-tabler.svg)](#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-csharp-ef/connecting.md b/src/tutorials/connect-to-mysql-in-csharp-ef/connecting.md deleted file mode 100644 index d347216..0000000 --- a/src/tutorials/connect-to-mysql-in-csharp-ef/connecting.md +++ /dev/null @@ -1,60 +0,0 @@ -# Connecting - Connect to MySql in C# and Entity Framework - -**Article Created: 21 May 2020** || **Last Edited: 25 November 2020** - -[`<<`](./introduction.html) || [`< Preparations`](./preparation.html) - ---- - -## Connection string -[![🔗](/img/link-tabler.svg)](#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. - -![MyServer root 127.0.0.1:3306](./img/connections.png) In this example, the user id is _root_ and the port is _3306_. - -## ASP.NET Startup -[![🔗](/img/link-tabler.svg)](#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(); - services.AddDbContext(opt => - { - opt.UseMySQL(CONNECTION_STRING); - }); -} -``` - -## DBContext child class -[![🔗](/img/link-tabler.svg)](#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-csharp-ef/introduction.md b/src/tutorials/connect-to-mysql-in-csharp-ef/introduction.md deleted file mode 100644 index 31de505..0000000 --- a/src/tutorials/connect-to-mysql-in-csharp-ef/introduction.md +++ /dev/null @@ -1,16 +0,0 @@ -# Introduction - Connect to MySql in C# and Entity Framework - -**Article Created: 21 May 2020** || **Last Edited: 25 November 2020** - -[`Preparation >`](./preparation.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 -[![🔗](/img/link-tabler.svg)](#Contents) - -1. [Introduction](./introduction.html) -2. [Preparation](./preparation.html) -3. [Connecting](./connecting.html) diff --git a/src/tutorials/connect-to-mysql-in-csharp-ef/preparation.md b/src/tutorials/connect-to-mysql-in-csharp-ef/preparation.md deleted file mode 100644 index e9f5d96..0000000 --- a/src/tutorials/connect-to-mysql-in-csharp-ef/preparation.md +++ /dev/null @@ -1,22 +0,0 @@ -# Preparation - Connect to MySql in C# and Entity Framework - -**Article Created: 21 May 2020** || **Last Edited: 25 November 2020** - -[`< Introduction`](./introduction.html) || [`Connecting >`](./connecting.html) - ---- - -## MySql -[![🔗](/img/link-tabler.svg)](#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 -[![🔗](/img/link-tabler.svg)](#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. - -- cgit v1.3.1-23-gd82d