ASP. NET Core project builds basic configuration and MySQL usage

Source: Internet
Author: User
Tags dot net

first, the development environment preparation

1, install Visual Studio 2015, I installed here is the professional version.

2. Install. NET Core SDK-related

You need to install Visual Studio update3 and net Core 1.0.0-vs tooling

Reference Link: http://www.cnblogs.com/fonour/p/5848933.html

Http://www.cnblogs.com/linezero/p/NETCoreMySQL.html

General description of the relevant files in the solution
    • Wwwroot storing static resources such as Js,css,images
    • Program.cs Application Portal
    • Startup.cs application-related startup item configuration, which contains the configureservices and configure two methods that are responsible for the configuration of the service, which is responsible for the configuration of the HTTP request pipeline.
    • Project.json the project's underlying configuration file
Second, MVC basic application

There are two ways to add MVC references.

1 by Project.json

Open the Project.json file, add MICROSOFT.ASPNETCORE.MVC references in the Dependencies department, and when you type, Visual studio will have automatic hints that are very handy.

"Dependencies": {    "Microsoft.NETCore.App":{      "version": "1.0.0",      "type": " Platform "    },    " Microsoft.AspNetCore.Diagnostics ":" 1.0.0 ",        " Microsoft.AspNetCore.Server.IISIntegration ":" 1.0.0 ",    " Microsoft.AspNetCore.Server.Kestrel ":" 1.0.0 ",    " Microsoft.Extensions.Logging.Console ":" 1.0.0 ",    " MICROSOFT.ASPNETCORE.MVC ":" 1.0.0 "  },

2 through NuGet

Search MICROSOFT.ASPNETCORE.MVC Add references through the NuGet Manager, or enter commands directly in Package Manager console

Install- package Microsoft. Aspnetcore. MVC

You can complete the addition of MVC references.

Iv. adding MVC Services and HTTP request pipeline processing

Add an MVC service

The Configureservices method for modifying Startup.cs is as follows

 Public void configureservices (iservicecollection services)        {            services. Addmvc ();        }

Add HTTP request Pipeline processing

The Configure method for modifying Startup.cs is as follows

   Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) {Logger            Factory.addconsole (); if(env. Isdevelopment ()) {//development Environment Exception handlingapp.            Usedeveloperexceptionpage (); }                       //use MVC to set a default routeApp. USEMVC (routes ={routes. MapRoute (Name:"default", Template:"{controller=login}/{action=index}/{id?}"                    );        }); }
V. Using MySQL in the project

MySQL Official drive: http://www.cnblogs.com/linezero/p/5806814.html

. NET core uses dapper to operate the MySQL database, and. NET core uses dapper.

Currently, the official does not have the. NET core MySQL driver, but there have been third-party changes to encapsulate the. NET core MySQL Connector preview.

Dapper has also been out of the. NET Core preview version.

Dapper Dot Net is a lightweight ORM, but the performance is very powerful.

With the. NET Core MySQL Connector We can operate the database directly using the ADO.

Third party MySQL Connector:https://github.com/sapientguardian/mysql-connector-net-netstandard

Dapper:https://github.com/stackexchange/dapper-dot-net

Add Reference

Using the NuGet console to add

Install-package SapientGuardian.MySql.Data-preinstall-package dapper-pre
or modify Project.json
{  "version":"1.0.0-*",  "buildoptions": {    "Emitentrypoint":true  },  "Dependencies": {    "Microsoft.NETCore.App": {      "type":"Platform",      "version":"1.0.1"    },    //MySQL Related configuration    "Dapper":"1.50.2",    "MySql.Data.Core":"7.0.4-ir-191"  },  "Frameworks": {    "netcoreapp1.0": {      "Imports":"Dnxcore50"    }  }}

using in ASP. NET Core

 Public Static voidMain (string[] args)            {Encoding.registerprovider (codepagesencodingprovider.instance); Mysqlconnection con = new Mysqlconnection ("server=127.0.0.1;database=test;uid=root;pwd=123456;charset= ' GBK"; Sslmode=none ");//New DataCon. Execute ("INSERT INTO user values (null, ' Test ', ' http://www.cnblogs.com/linezero/')"); //New data return self-increment ID            varId=con. queryfirst<int> ("INSERT INTO user values (null, ' Linezero ', ' http://www.cnblogs.com/linezero/'); select last_insert_id ();"); //Modifying DataCon. Execute ("Update user Set UserName = ' linezero123 ' where Id = @Id",New{Id =ID}); //Querying Data            varList=con. Query<user> ("SELECT * from user"); foreach(varIteminchlist) {Console.WriteLine ($"User name: {item. UserName} Link: {Item. URL}"); }            //Delete DataCon. Execute ("Delete from user where Id = @Id",New{Id =ID}); Console.WriteLine ("results after data is deleted"); List= Con. Query<user> ("SELECT * from user"); foreach(varIteminchlist) {Console.WriteLine ($"User name: {item. UserName} Link: {Item. URL}");        } console.readkey (); }

ASP. NET Core project builds basic configuration and MySQL usage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.