Summary Files Commits Branches @commit @diff @stats
path: root / src / blog / connect-to-mysql-in-dotnet-ef / index.du
diff options
context:
space:
mode:
1 files changed, 32 insertions, 28 deletions
-rw-r--r--src/blog/connect-to-mysql-in-dotnet-ef/index.du60
diff --git a/src/blog/connect-to-mysql-in-dotnet-ef/index.du b/src/blog/connect-to-mysql-in-dotnet-ef/index.du
index e745f16..142d398 100644
--- a/src/blog/connect-to-mysql-in-dotnet-ef/index.du
+++ b/src/blog/connect-to-mysql-in-dotnet-ef/index.du
@@ -1,66 +1,70 @@
-# Connect to MySql in .NET and Entity Framework
+'def title Connect to MySql in .NET and Entity Framework
+'def desc Connecting a .NET application to a MySql database (with Entity Framework) can be tricky. Here is what works for me.
-'define expected-reading 3 min
-'define created 21 May 2020
-'define edited 19 June 2021
-[$pagenav]
+# {title}
+
+{pagenav 3 min;21 May 2020;19 June 2021}
:contents
.numbered
-1. [url #nuget-packages NuGet packages]
-2. [url #connection-string Connection string]
-3. [url #connecting Connecting]
+1. {uhref NuGet packages}
+2. {uhref Connection string}
+3. {uhref Connecting}
.numbered
- 1. [url #asp.net-startup ASP.NET Startup]
- 2. [url #dbcontext-child-class DBContext child class]
+ 1. {uhref ASP.NET Startup}
+ 2. {uhref DBContext child class}
-I have found that connecting a [`.NET`] application to a [`MySql`] database (with Entity Framework) can be tricky. Here is what works for me.
+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, [url https://www.nuget.org/packages/MySql.Data.EntityFrameworkCore/ MySql.Data.EntityFrameworkCore] has been [=depricated=] since [*October 2020*]. If you need setup instructions with it, read [url https://gitlab.com/Syndamia/syndamiadotcom/-/blob/0510e29526c6d7681587fff4c98a339e8c69e9b5/src/blog/connect-to-mysql-in-csharp-ef/article.md this].
+Also, {url MySql.Data.EntityFrameworkCore;https://www.nuget.org/packages/MySql.Data.EntityFrameworkCore/} has been {b depricated} since {i October 2020}. If you need setup instructions with it, read {url this;https://gitlab.com/Syndamia/syndamiadotcom/-/blob/0510e29526c6d7681587fff4c98a339e8c69e9b5/src/blog/connect-to-mysql-in-csharp-ef/article.md}.
## NuGet packages
-You'll need to install [url https://www.nuget.org/packages/Pomelo.EntityFrameworkCore.MySql/ Pomelo.EntityFrameworkCore.MySql]. It provides the stuff necessary for connecting and using a [`MySql`] database.
+You'll need to install {url 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
-There are [url https://www.connectionstrings.com/mysql/ a lot] of ways to setup the connection string but I've found that this one works the best:
+There are {url 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.
+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.
+
+{img MyServer root 127.0.0.1:3306;./img/connections.png}
-[image ./img/connections.png MyServer root 127.0.0.1:3306] In this example, the user id is _root_ and the port is _3306_.
+In this example, the user id is {i root} and the port is {i 3306}.
## Connecting
-To connect, you'll need to call the [`UseMySql`] method, either in [url #asp.net-startup ASP.NET Startup] or in a [url #dbcontext-child-class DBContext child class].
+To connect, you'll need to call the {` UseMySql} method, either in {url ASP.NET Startup;#asp.net-startup} or in a {url DBContext child class;#dbcontext-child-class}.
-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`].
+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}.
-[image ./img/about-workbench.png]
-[image ./img/workbench-version.png]
+{img ./img/about-workbench.png}
+{img ./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:
+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:
-```c#
+```csharp
+'emplace
.UseMySql(CONNECTION_STRING, new MySqlServerVersion(new System.Version(8, 0, 21)))
+'endemplace
-Where [`CONNECTION_STRING`] is a (constant) variable that contains your connection string, and 8, 0, 21 are the version numbers.
+Where {` CONNECTION_STRING} is a (constant) variable that contains your connection string, and 8, 0, 21 are the version numbers.
### ASP.NET Startup
-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.
+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:
-```c#
+```csharp
// 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(...);
@@ -69,9 +73,9 @@ public void ConfigureServices(IServiceCollection services)
### DBContext child class
-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.
+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.
-```c#
+```csharp
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMySql(...);