diff options
3 files changed, 22 insertions, 30 deletions
diff --git a/src/tutorials/connect-to-mysql-in-csharp-ef/connecting.md b/src/tutorials/connect-to-mysql-in-csharp-ef/connecting.md index 650d6c2..9292bd5 100644 --- a/src/tutorials/connect-to-mysql-in-csharp-ef/connecting.md +++ b/src/tutorials/connect-to-mysql-in-csharp-ef/connecting.md @@ -1,30 +1,31 @@ # Connect to MySql in C# and Entity Framework - Connecting -**Article Created: 21 May 2020** || **Last Edited: 10 September 2020** +**Article Created: 21 May 2020** || **Last Edited: 25 November 2020** [`<<`](./introduction.html) || [`< Preparations`](./preparation.html) -## [*Due for rework*](/rework.html) - --- ## Connection string -You can find many ways to impliment your connection string from [here](https://www.connectionstrings.com/mysql/). -From experience, this one works the best: +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 don't know your port or user id, just look at the connection you use to connect to the database. +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 -If you are making an ASP.NET application, you will need to alter your Startup.cs file. -Your ConfigureServices method should currenly look something like this: +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. @@ -34,24 +35,23 @@ public void ConfigureServices(IServiceCollection services) services.AddTransient<UserServices, UserServices>(); services.AddDbContext<AppDbContext>(opt => { - opt.UseSQL(CONNECTION_STRING); + opt.UseMySQL(CONNECTION_STRING); }); } ``` -You need to change `.UseSQL` to `.UseMySQL` (refer to [preparations](./preparation.html) if you haven't already). - ## DBContext child class -If you want to connect a DBContext child class to a MySQL databse, the best way is to override the `OnConfiguring` method. In the override, in `optionsBuilder`, call the `.UseMySQL` method (refer to [preparations](./preparation.html) if you haven't already). -Example: +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(DataBaseContexts.ConnectionStringMySQL); + optionsBuilder.UseMySQL(CONNECTION_STRING); } ``` -Now you should be connected to your database. +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 index ce03ee3..e2e882a 100644 --- a/src/tutorials/connect-to-mysql-in-csharp-ef/introduction.md +++ b/src/tutorials/connect-to-mysql-in-csharp-ef/introduction.md @@ -1,18 +1,12 @@ # Connect to MySql in C# and Entity Framework - Introduction -**Article Created: 21 May 2020** || **Last Edited: 10 September 2020** +**Article Created: 21 May 2020** || **Last Edited: 25 November 2020** [`Preparation >`](./preparation.html) -## [*Due for rework*](/rework.html) - --- -Connecting to `MySql` through `C#` can be quite tricky. I and many others have wasted too much time trying to figure this out. For that reason, I am making this tutorial. - -First are preparations in which I will talk about what you need to have for this to work. - -Second is the actual connecting. This covers changes in code and the all mighty connection string. +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 diff --git a/src/tutorials/connect-to-mysql-in-csharp-ef/preparation.md b/src/tutorials/connect-to-mysql-in-csharp-ef/preparation.md index 0e72fa6..2736b51 100644 --- a/src/tutorials/connect-to-mysql-in-csharp-ef/preparation.md +++ b/src/tutorials/connect-to-mysql-in-csharp-ef/preparation.md @@ -1,22 +1,20 @@ # Connect to MySql in C# and Entity Framework - Preparation -**Article Created: 21 May 2020** || **Last Edited: 10 September 2020** +**Article Created: 21 May 2020** || **Last Edited: 25 November 2020** [`< Introduction`](./introduction.html) || [`Connecting >`](./connecting.html) -## [*Due for rework*](/rework.html) - --- ## MySql -First of all, you need to insall MySql. You can do that from [here](https://dev.mysql.com/downloads/installer/). This is the installer. Skip this step, if you have already done it. +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 -For an Entity Framework, [Microsoft.EntitiyFrameworkCore](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore) is the goto packet in this tutorial. +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/). -Although [Microsoft.EntitiyFrameworkCore](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore) does offer connectivity with MS SQL server, **it doesn't offer the specific things for MySql**. +[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. -That's why we need to also install another package: [MySql.Data.EntitiyFrameworkCore](https://www.nuget.org/packages/MySql.Data.EntityFrameworkCore/). +[MySql.Data.EntityFrameworkCore](https://www.nuget.org/packages/MySql.Data.EntityFrameworkCore/) is what enables Microsoft.EntityFrameworkCore to connect to a `MySQL` database. |