1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
<!DOCTYPE html>
<html lang="en-us">
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="Code you need to write to connect to MySql in C# and Entitiy Framework">
<meta name="keywords" content="programming, C#, tutorial, mysql, entity, framework, database, asp.net, dbcontext, connectionstring, connection">
<meta name="author" content="Kamen Mladenov">
<meta name="robots" content="index, follow">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" href="../../images/favicon.png" type="image/png"/>
<link rel="shortcut icon" href="../../images/favicon.png" type="image/png"/>
<title>Connect to MySql in C# and Entitiy Framework - Connecting</title>
<link rel="stylesheet" href="../../styles.css">
<link rel="stylesheet" href="../../styles-multiple-page-articles.css">
</head>
<body>
<div class="header">
<div class="article-title">Connect to MySql in C# and Entitiy Framework</div>
<a href="./introduction.html" class="other-tab">Introduction</a>
<a href="./preparation.html" class="other-tab">Preparation</a>
<a href="./connecting.html" class="selected-tab">Connecting</a>
<a href="../index.html" class="close" style="margin-left: 403px;" title="Back to Tutorials"><img src="../../images/ui/close.png" alt="Back" width="32" height="32"></a>
</div>
<div class="page">
<h2>Connection string</h2>
<p>
You can find many ways to impliment your connection string from <a href="https://www.connectionstrings.com/mysql/" class="hyperlink">here</a>.
From experience, this one works the best: <br>
<p class="code">
"server=localhost; port=YOUR-PORT; user id=YOUR-USER-ID; password=YOUR-PASSWORD; database=NAME-OF-DATABASE; SslMode=none"
</p>
If you don't know your port or user id, just look at the connection you use to connect to the database.
</p>
<p class="tutorial-p">
<img src="./images/connections.png" alt="MyServer root 127.0.0.1:3306" class="tutorial-img" width="262" height="152">
<span class="tutorial-text">
In this example, the user id is <i>root</i> and the port is <i>3306</i>.
</span>
</p>
<br>
<h2>ASP.NET Startup</h2>
<p>
If you are making an ASP.NET application, you will need to alter your Startup.cs file.
</p>
<p>
Your ConfigureServices method should currenly look something like this:
<p class="code">
 // This method gets called by the runtime. Use this method to add services to the container. <br>
 public void ConfigureServices(IServiceCollection services) <br>
 { <br>
  services.AddControllersWithViews(); <br>
  services.AddTransient<UserServices, UserServices>(); <br>
  services.AddDbContext<AppDbContext>(opt => <br>
  { <br>
   opt.UseSQL(CONNECTION_STRING); <br>
  }); <br>
 }
</p>
You need to change <span class="code">.UseSQL</span> to <span class="code">.UseMySQL</span> (refer to <a href="./preparations" class="hyperlink">preparations</a> if you haven't already).
</p>
<br>
<h2>DBContext child class</h2>
<p>
If you want to connect a DBContext child class to a MySQL databse, the best way is to override the <span class="code">OnConfiguring</span> method. In the override, in <span class="code">optionsBuilder</span>, call the <span class="code">.UseMySQL</span> method (refer to <a href="./preparations" class="hyperlink">preparations</a> if you haven't already).
</p>
<p>
Example:
<p class="code">
 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) <br>
 { <br>
  optionsBuilder.UseMySQL(DataBaseContexts.ConnectionStringMySQL); <br>
 } <br>
</p>
</p>
</div>
<br>
<footer class="footer">
<a href="https://github.com/Syndamia" target="_blank" class="hyperlink-footer" title="GitHub"><img src="../../images/logos/github.png" alt="GitHub" width="32" height="32"></a>
<a href="https://stackoverflow.com/users/12036073/syndamia" target="_blank" class="hyperlink-footer" title="StackOverflow"><img src="../../images/logos/stackoverflow.png" alt="StackOverflow" width="32" height="32"></a>
<a href="https://bg.linkedin.com/in/kamen-mladenov-18563b1a3?trk=people-guest_people_search-card" target="_blank" class="hyperlink-footer" title="Linkedin"><img src="../../images/logos/linkedin.png" alt="Linkedin" width="32" height="32"></a>
<hr width="350px" noshade>
<a href="../index.html" class="hyperlink-footer">Tutorials</a> |
<a href="../../projects/index.html" class="hyperlink-footer">Projects</a> |
<a href="../../about.html" class="hyperlink-footer">About</a> |
<a href="../../resume.html" class="hyperlink-footer">Resume</a>
<hr width="350px" noshade>
<a href="https://github.com/Syndamia/syndamiadotcom/blob/master/LICENSE" target="_blank" class="hyperlink-footer">Website License</a> |
<a href="../../credit.html" target="_blank" class="hyperlink-footer">Credit</a>
</footer>
</body>
</html>
|