Since as middleware, need, before, and the front end of the data interaction, after the data with the database interaction, in the previous step, we have sent out the data, as long as the front-end calls to the data is good, below we describe how to connect to the database. In part (a), I have also set up the server and MySQL, here to explain, whether it is built by the local virtual machine or the cloud server, all can be, the same.
Let us think about it before you do it, we want to connect MySQL, but why do we say that even, people MySQL do not face it?
So in MySQL, we're going to create a user (we install and configure a user who has already created a myuser in MySQL), then give the user permission to manipulate the database, and then create the user's connection string inside the Webapi, as to what the connection string is, I'll talk about it later.
At the same time, because there is no built-in MySQL tool in. NET core, we will first install a tool to manipulate the database, this is EF core (Entity Framework Core), the term called ORM tool (Object Relational Mapping), similar to dapper (this is the first ORM tool I used). Let's install it first.
1) Tool-nuget Package Manager-NuGet package for management solution
2) Browse-Enter mysql-find MySQL.Data.EntityFramworkCore, install
3) Open the Startup.cs-->configureservices method in the directory to configure the database
public void Configureservices (iservicecollection services) { //Database configuration services. adddbcontext<join> (options = options. Usemysql (configuration.getconnectionstring ("defaultconnection")); Services. Addmvc (); }
This may prompt the error, but do not be afraid, the join (that is, the blue word above) without the tube, point "usemysql", and then click on the left side of the yellow light bulb, click on "Using Microsoft.entityframeworkcore", And then one of our mistakes disappears,
Then the top will also add a line, the gray expression has not been used,
If you look uncomfortable, you can right-click and select "Delete and sort using by".
4) then see the orange yellow font without "defaultconnection", this is our connection string ... Variable, I would normally place the connection string in the Appsetting.json with the Startup.cs directory
{" ConnectionStrings": { "defaultconnection": "server=192.168.253.129;userid=myuser;pwd=123456;port= 3306;database=example;sslmode=none; " }, //above is the database connection string " Logging ": { " includescopes ": false, " Debug ": {" LogLevel ": {" Default ":" Warning " } }, " Console ": {" LogLevel ": { " Default ":" Warning "}}} }
As for the "server=192.168.253.129;userid=myuser;pwd=123456;port=3306;database=example;sslmode=none;" is the legendary connection string, but we need to be in the server MySQL set up, because this part belongs to MySQL, so I do not write, for details, please refer to the installation and configuration of MySQL
Here's a little bit about the parameters
IP address of the server where the Server:mysql resides
UserID: The user name that is authorized
PWD: Password for the authorized user name
Port:mysql port number, default 3306
Database: Databases to be used
Sslmode: As for why this parameter, the online explanation is "link string to add Sslmode=none otherwise will report error : SSL not supported in this WinRT release." Because I have been added, so I have not encountered a mistake.
Walk, Next, create a related data Model (models)
(ii) 2, WEBAPI connection MySQL