From 9c04af3e31e88b96efe48867ea741a4d47ba937b Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 21 May 2020 22:06:15 +0300 Subject: Added a tutorial on MySql and C# and Entity Framework. Items in projects and tutorials will be separated by a blank line. Updated formating on some files. --- .../connect-to-mysql-in-csharp-ef/connecting.html | 94 +++++++++++++++++++++ .../images/connections.png | Bin 0 -> 10067 bytes .../introduction.html | 53 ++++++++++++ .../connect-to-mysql-in-csharp-ef/preparation.html | 55 ++++++++++++ website/tutorials/index.html | 5 ++ 5 files changed, 207 insertions(+) create mode 100644 website/tutorials/connect-to-mysql-in-csharp-ef/connecting.html create mode 100644 website/tutorials/connect-to-mysql-in-csharp-ef/images/connections.png create mode 100644 website/tutorials/connect-to-mysql-in-csharp-ef/introduction.html create mode 100644 website/tutorials/connect-to-mysql-in-csharp-ef/preparation.html (limited to 'website/tutorials') diff --git a/website/tutorials/connect-to-mysql-in-csharp-ef/connecting.html b/website/tutorials/connect-to-mysql-in-csharp-ef/connecting.html new file mode 100644 index 0000000..dfbba29 --- /dev/null +++ b/website/tutorials/connect-to-mysql-in-csharp-ef/connecting.html @@ -0,0 +1,94 @@ + + + + + + Connect to MySql in C# and Entitiy Framework - Connecting + + + + + +
+
Connect to MySql in C# and Entitiy Framework
+ + Introduction + Preparation + Connecting + + Back +
+ +
+

Connection string

+

+ You can find many ways to impliment your connection string from here. + From experience, 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. +

+

+ MyServer root 127.0.0.1:3306 + + 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: +

+  // 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.UseSQL(CONNECTION_STRING);
+   });
+  } +

+ You need to change .UseSQL to .UseMySQL (refer to preparations 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 if you haven't already). +

+

+ Example: +

+  protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
+  {
+   optionsBuilder.UseMySQL(DataBaseContexts.ConnectionStringMySQL);
+  }
+

+

+
+ +
+ + + + diff --git a/website/tutorials/connect-to-mysql-in-csharp-ef/images/connections.png b/website/tutorials/connect-to-mysql-in-csharp-ef/images/connections.png new file mode 100644 index 0000000..aa7b408 Binary files /dev/null and b/website/tutorials/connect-to-mysql-in-csharp-ef/images/connections.png differ diff --git a/website/tutorials/connect-to-mysql-in-csharp-ef/introduction.html b/website/tutorials/connect-to-mysql-in-csharp-ef/introduction.html new file mode 100644 index 0000000..d4812fc --- /dev/null +++ b/website/tutorials/connect-to-mysql-in-csharp-ef/introduction.html @@ -0,0 +1,53 @@ + + + + + + Connect to MySql in C# and Entitiy Framework - Introduction + + + + + +
+
Connect to MySql in C# and Entitiy Framework
+ + Introduction + Preparation + Connecting + + Back +
+ +
+

+ 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. +

+
+ +
+ + + + diff --git a/website/tutorials/connect-to-mysql-in-csharp-ef/preparation.html b/website/tutorials/connect-to-mysql-in-csharp-ef/preparation.html new file mode 100644 index 0000000..3e1196f --- /dev/null +++ b/website/tutorials/connect-to-mysql-in-csharp-ef/preparation.html @@ -0,0 +1,55 @@ + + + + + + Connect to MySql in C# and Entitiy Framework - Preparation + + + + + +
+
Connect to MySql in C# and Entitiy Framework
+ + Introduction + Preparation + Connecting + + Back +
+ +
+

MySql

+

+ First of all, you need to insall MySql. You can do that from here. This is the installer. Skip this step, if you have already done it. +

+

NuGet packages

+

+ For an Entity Framework, Microsoft.EntitiyFrameworkCore is the goto packet in this tutorial. +

+

+ Although Microsoft.EntitiyFrameworkCore does offer connectivity with SQL server, it doesn't offer anything for MySql. Thats why we need another package: MySql.Data.EntitiyFrameworkCore. +

+
+ +
+ + + + diff --git a/website/tutorials/index.html b/website/tutorials/index.html index a02f3fd..447e908 100644 --- a/website/tutorials/index.html +++ b/website/tutorials/index.html @@ -5,6 +5,7 @@ Syndamia - Tutorials + @@ -19,6 +20,10 @@
GtkSharp 2.0 [COMING SOON] +
+ + Connect to MySql in C# and Entity Framework +

I have some tutorial-style notes in my references repo. Warning: some stuff might be wrong.

-- cgit v1.3.1-23-gd82d