.netCore builds WebAPI, and MySQL, SQL server database connection mode

Keywords: Code First, Database First

1. Project creation

1.1 Installation

            When we build the .netcore program, the first thing is whether the system environment is completed.

            Download the corresponding .NETCORE SDK and install it. Microsoft's installation is very simple in terms of windows, basically one-click installation, so I don't need to talk about it here.

1.2 Development tools

            VisualStudio 2017

1.3 Establish project engineering

            Click the file to create the project, and the following icon will appear

            Select the red circled part, and then the following icon will appear

            Select API, and the authentication is no authentication. After clicking OK, the corresponding template project is automatically generated.

            The file format is as follows:

             

            Connected Services: Connected services.

            Properties: Set some properties.

            wwwroot: Storage location of static files.

            Dependency: the assembly that needs to be added.

            Controller: Controller folder, used for business writing code, we basically need to transmit and receive data in this file. Correspondingly, we can also create each file for hierarchical differentiation, or create a separate category project for hierarchical differentiation, such as the control layer, the service business layer, the data connection layer, and the model entity layer.

             appsettings: configuration file, here you can write some constant settings, and then read them in the program.

             Program: Project startup file.

             Startup: Project configuration settings file.

2. Database connection

2.1. MySQL database connection

            First of all, we use nuget to download and install the MySQL assembly. If there is no network environment, you can refer to my other article (about using nuget to install the assembly offline), as shown below:

            Then we create a data table structure, as shown in the figure below (a simple example here):

          

         

            Create the corresponding entity class in C#, and create a class to inherit dbContext. dbContext is a very important link in the EF framework and is to establish a bridge between the entity class and the database. The specific principles are not repeated here.

            The specific created style is shown in the figure below:

           

 

In this way, we can operate the database, test it:

            It indicates that our database has been successfully connected, and the corresponding data can be successfully output for related display.

            In view of the above connection properties, we should not write the connection properties in the code, so we have to design, write the connection properties in the configuration file, for appsettings.json, Startup.cs, CoreDbContext.cs Make the relevant configuration, as shown in the figure:

            

           

           

            The part marked in red in the figure is the part we need to modify or add. At this time, coreDbContext has been injected into the container for operation, and the injection method of Context in the container is instantaneous injection, so dependency injection will be used later In many cases, when the context is used in the data layer, the corresponding injection needs to be designed in the form of instantaneous injection, so I won't mention too much here.

            Let’s test it. Now we don’t need to perform the context new operation ourselves. Since we have already carried out the form of dependency injection when we set it up at the beginning, however, there is only construction injection in .netCore, and no attribute injection, so we Just use the structure injection method, as shown in the following figure:

            At this point, it is proved that we can also perform data operations using this method.

2.2 SQL server database connection operation

            The specific connection method is basically the same as the above, but the properties of connecting to sqlServer are set to:

2.3 Database First and Code First operations

            Code First: Complete the database generation from the model through EF migration. Therefore, the database does not need to design related tables, and directly perform the above operations outside the database and build tables in C#. 

            Download and install Microsoft.EntityFrameworkCore.Tools

            Open the VS 2017 menu tool -> nugget package manager -> package management console.

            Enter Add-Migration MyFirstMigration, and then enter Update-Database to execute, and Done appears to indicate that the database is successfully created.

            Database First: Generate a model through the existing database (SQLServer is the main method here, and MySQL has not found a corresponding assembly to perform this operation).

            安装Microsoft.EntityFrameworkCore.SqlServer

            Install Microsoft.EntityFrameworkCore.Tools

            Install Microsoft.EntityFrameworkCore.SqlServer.Design

            Execute the following commands:

            Scaffold-DbContext “Server=;Database=DatabaseName;UID =;PWD=” Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

            This will be output to the Models folder

Guess you like

Origin blog.csdn.net/zouzh/article/details/109485181