From 6a01c64d9358a3457f22785308215ef4c12e6679 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 19 Feb 2022 16:21:20 +0200 Subject: With folders and hard links shortened overall web links and updated all files with links in them --- .../connect-to-mysql-in-dotnet-ef/article.md | 85 ---------------------- .../connect-to-mysql-in-dotnet-ef/index.md | 85 ++++++++++++++++++++++ 2 files changed, 85 insertions(+), 85 deletions(-) delete mode 100644 src/tutorials/connect-to-mysql-in-dotnet-ef/article.md create mode 100644 src/tutorials/connect-to-mysql-in-dotnet-ef/index.md (limited to 'src/tutorials/connect-to-mysql-in-dotnet-ef') diff --git a/src/tutorials/connect-to-mysql-in-dotnet-ef/article.md b/src/tutorials/connect-to-mysql-in-dotnet-ef/article.md deleted file mode 100644 index 4b3a5da..0000000 --- a/src/tutorials/connect-to-mysql-in-dotnet-ef/article.md +++ /dev/null @@ -1,85 +0,0 @@ -# Connect to MySql in .NET and Entity Framework - - -[**Article Source**](https://gitlab.com/Syndamia/syndamiadotcom/-/blob/master/src/tutorials/connect-to-mysql-in-dotnet-ef/article.md) || [**GitLab Discussion**](https://gitlab.com/Syndamia/syndamiadotcom/-/issues/3) -
-**Created: 21 May 2020** || **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 -
- -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 -[![🔗](/img/link-tabler.svg)](#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 -[![🔗](/img/link-tabler.svg)](#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. - -![MyServer root 127.0.0.1:3306](./img/connections.png) In this example, the user id is _root_ and the port is _3306_. - -## Connecting -[![🔗](/img/link-tabler.svg)](#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`. - -![](./img/about-workbench.png) -![](./img/workbench-version.png) - -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 -[![🔗](/img/link-tabler.svg)](#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(...);
-    });
-}
-

- -### DBContext child class -[![🔗](/img/link-tabler.svg)](#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(...);
-}
-

diff --git a/src/tutorials/connect-to-mysql-in-dotnet-ef/index.md b/src/tutorials/connect-to-mysql-in-dotnet-ef/index.md new file mode 100644 index 0000000..4b3a5da --- /dev/null +++ b/src/tutorials/connect-to-mysql-in-dotnet-ef/index.md @@ -0,0 +1,85 @@ +# Connect to MySql in .NET and Entity Framework + + +[**Article Source**](https://gitlab.com/Syndamia/syndamiadotcom/-/blob/master/src/tutorials/connect-to-mysql-in-dotnet-ef/article.md) || [**GitLab Discussion**](https://gitlab.com/Syndamia/syndamiadotcom/-/issues/3) +
+**Created: 21 May 2020** || **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 +
+ +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 +[![🔗](/img/link-tabler.svg)](#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 +[![🔗](/img/link-tabler.svg)](#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. + +![MyServer root 127.0.0.1:3306](./img/connections.png) In this example, the user id is _root_ and the port is _3306_. + +## Connecting +[![🔗](/img/link-tabler.svg)](#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`. + +![](./img/about-workbench.png) +![](./img/workbench-version.png) + +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 +[![🔗](/img/link-tabler.svg)](#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(...);
+    });
+}
+

+ +### DBContext child class +[![🔗](/img/link-tabler.svg)](#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(...);
+}
+

-- cgit v1.3.1-26-gb76a