How is a cross-platform NET core

The laptop has been installed with two systems (Windows 10 and Ubuntu 16.04) for almost half a year. I usually like to switch to Ubuntu and play Linux. I am familiar with installing and configuring Linux commands, shell scripts, and various applications on Linux. , Run. The feeling of using it is that Linux is really pretty good. The efficiency of the work after getting familiar with the basic operations is not a star, and now the use of Linux has been quite extensive. With the implementation of the cross-platform .NET strategy (.NET Core), .NET programmers must also be familiar with .NET development on Linux. Therefore, in this blog we will explain the development of asp.net core mvc on Ubuntu.

1. Preparation

First, introduce my development environment:

1. Operating system: Ubuntu 16.04

Ubuntu is the current mainstream Linux desktop system, the official website: https: //www.ubuntu.com/download。

2. Development tools: vscode 1.22.2

vscode (Visual Studio Code, known as vscode) is a cross-platform code editor from Microsoft that can support multiple programming languages ​​(C #, C ++, Python, Node.js, etc.) by installing extensions. If you think the vs is too hard, you can try this easy vscode, the official website: https: //code.visualstudio.com/。

3. Operating environment: .net Core 2.1

.net core is the cross-platform version of .net officially released by Microsoft. We develop cross-platform web applications. The asp.net core mvc used requires the installation of the .net core operating environment. The official website: https: //dotnet.github.io/

4. Database: MySQL 5.7

There is no need to introduce mysql, one of the three giants in the database field (oracle, mysql, sqlserver), a free cross-platform, open source database. The integrated MySQL Workbench can operate MySQL very comfortably. It is said that Microsoft SQL Server started the Linux version too, there is a chance to try again in the future. Official website: https: //www.mysql.com/。

5 、 web server : jexus 5.8.3

After releasing the developed asp.net core MVC application, we need a web server to provide the operating environment. Jexus is a free, open source Asp.net web server running on Linux. There is no need to introduce any more about Jexus here. The detailed content can be found on the official website: https: //www.jexus.org/Inquiries.

Installation of the above environment is limited here, so I won't focus on that. I believe readers can successfully complete the preparation of the above environment according to official documents (check carefully) or Baidu. The goal of our blog is to use vscode to develop the asp.net core application under Ubuntu and to complete the data persistence using ef core. The database uses cross-platform free open source MySQL and the final asp.net core MVC application is released. Host in jexus and let jexus start randomly so that as soon as we start the computer we can open our core asp.net MVC application.

Regarding this project, I'm going to choose a ready-made solution here - the registration application that comes from the book "Pro Entity Framework Core 2 for ASP.NET Core MVC". There is a free electronic English version online. By the way, I recommend this book here too. This book features some short development cases for beginners that come with detailed implementation steps and screenshots, and the learning experience is very good (here I have to admire the tutorial written by foreigners). .

Second, use vscode to develop the core asp.net application

In Ubuntu, press Ctrl + Alt + T to open the console, enter the command dotnet –version, view the .net core version, and make sure it is installed correctly.

Then we enter the dotnet new mvc command to create an asp.net core mvc application.

If you see that the restore was successful as shown above, then the build is successful.

Then we use vscode to open that directory. You can see the project directory structure as shown below.

When prompted, ask you to install the C # extension and click Yes to install.

As you can see from the screenshot, the project structure of asp.net core mvc that is shown to us by vscode is very clear. Controller corresponds to controller, models corresponds to model class, views corresponds to view, wwwroot stores some static resources including js, css, img, lib (bootstrap, jquery). Some configuration files are JSON format files, and knowing JSON is not a problem. There is also a csproj file that is common to all C # projects. There are also two cs files, namely Program.cs and Startup.cs, where the main method in Program.cs is the running input of the program and Startup.cs contains some configuration information at the beginning of the project, e.g. B. Adding MVC support and routing settings and so on.

The asp.net net core application shown in vscode is a complete and ready-to-use version. We open the Program.cs file in vscode and add a line of code to the BuildwebHost method:

Then press Ctrl + F5 on the vscode interface (or click Start Debug Without Debugging on the vscode menu bar) to run the project directly and you will have access to the core asp.net application we created .

The standard URL is: localhost: 5001, localhost: 5002 can also be called. We did this by adding the UseUrls method.

The screenshot of the vscode status at this point is as follows:

We can see through the navigation bar in the middle that the vscode is now running. We can use the keyboard shortcut Shift + F5 or click the red square to stop the project.

Or change to the asp.net core project directory and enter the dotnet run command to start and run the asp.net core project.

Three, add EF Core

To use EF Core to connect MySQL in the vscode project, you need to add references to the Microsoft.EntityFrameworkCore and MySql.Data.EntityFrameworkCore packages.

Click the menu bar-View-Command Palette-Nuget Package Manager: Add Package, type Microsoft.EntityFrameworkCore and press Enter. Select the version and add the reference automatically.

The installation method for the MySql.Data.EntityFrameworkCore package is the same. Once installed, vscode will prompt you in the lower right corner to restore the project. Click on Restore.

That way we can introduce a namespace for the UseMySQL method.

Note: The specific code of the program is not detailed here and the full code of the project is published at the end of this article.

After development is complete, we can open the console, go to the asp.net core directory and generate the database with the ef core command.

The command entered is: dotnet ef migrations add Initial, but the following error message occurs:

The error message is that no matching dotnet ef command was found. We open the project's csproj file and add the following configuration information.

The full configuration information after editing is as follows:

We run the command dotnet ef migrations add Initial again on the console. Another error occurred. The error screenshot is as follows:

Error message: Concrete type assignment cannot be converted to relationship type assignment type. I think the version of Microsoft.EntityFrameworkCore is higher, the writing of the API has been adjusted, and new type conversion code needs to be written. Let me ask this question first, and then I'll study it later. Baidu on the internet said that the version of Microsoft.EntityFrameworkCore is too high. We used version 2.1.1 and reduced it to version 2.0.2.

Same operation, vscode menu bar -view command palette-Nuget Package Manager: add package, type Microsoft.EntityFrameworkCore and press Enter. Select version 2.0.2 to automatically add references. The final modified csproj file is as follows:

Run the ef core migration command again, this time it was finally successful.

At this point we return to the vscode editor and find that there is already a migration folder in the project.

Enter the following command at the console interface: dotnet ef database update.

You can see that the database and table were successfully created in mysql via ef core. We open the MySQL Workbench and look at the content of the database test.

OK! So far the development problems have been solved. The last step is to publish our project and host it on the web server.

Fourth, release, deployment

Publishing the asp.net core MVC project is easy. Just use the Publish command. We can first create a new directory to publish the file to that path.

Publish command: dotnet Publish -o / home / guwei4037 / webapp. This will publish the project in / home / guwei4037 / webapp

After the project is published, we need to deploy the project to jexus.

We put jexus in the directory / usr / jexus. Then edit the contents of the default file in / usr / jexus / siteconf.

Enter the port port number as port 80, root as the / root directory and the following directory / home / guwei4037 / webapp as the project share path, separated by a space in the middle. NoLog = true means that no log is recorded, so the website access speed is faster. The disadvantage is that no log is recorded. If there are problems, the log information cannot find it quickly. AppHost = {cmd = dotnet /home/guwei4037/webapp/firstcore.dll;root=/home/guwei4037/webapp;port=5001,5002}, where cmd specifies how the program is executed, the path behind it is the start file and root The port configuration of the root directory supports multiple ports.

jexus will be configured and restarted. We can open localhost to see if application access is normal.

can be opened normally.

Hurry up to use the features inside, all are fine.



jexus successfully hosted our application. How can Jexus start randomly in the last step? Main points of installation and configuration in Jexus (https://www.linuxdot.net/bbsfile-3084) gave the answer:

Start automatically at boot:
Add the following command line to the startup script in /etc/rc.local or the class test:
mono /usr/jexus/jws.exe> ​​/ dev / null 2> & 1 &
or
/usr/jexus/jws.start #Recommended

Since we quoted Bootstrap, use the ifconfig command to check the IP address of the computer running Ubuntu and enter that IP address using your mobile browser to register for the conference on the mobile phone.
Reference materials:

1. Solve MySQL Chinese mangled completely

2. Main points of installing and configuring Jexus

3. Asp.net Core Release and Deployment

4 、 Table ‘db .__ EFMigrationsHistory’ doesn’t exist

5 、 Creating a Database with Code First in EF Core

Project source code download address:

Link: https: //pan.baidu.com/s/1buXY97zkvaMajFTYCOoc_Q Password: 4rpa