Since Netcore Open source, then also use the open source MySQL database? Of course Netcore not only with the MSSQL database. Today I'll explain how Netcore uses MySQL for development.
Create a new Netcore project first
Then write two classes and put them in the model.
public class Lexan { private lexancontext lexancontext; public string Name {get; set;} public int Sex {get; set;} public int Age {get; set;} }
public class Lexancontext {public string ConnectionString {get; set;} Public Lexancontext (String connectionString) {connectionString = connectionString; } private Mysqlconnection getconnection () {return new mysqlconnection (ConnectionString); } public list<lexan> Getlexan () {list<lexan> List = new list<lexan> (); using (Mysqlconnection connection=getconnection ()) {connection. Open (); Mysqlcommand command = new Mysqlcommand ("SELECT * from Lexan", connection); using (Mysqldatareader Reader=command. ExecuteReader ()) {while (reader. Read ()) {list. ADD (New Lexan () {Name=reader. GetString ("Name"), Sex=reader. GetInt32 ("Sex"), Age=reaDer. GetInt32 ("Age")}); }}} return list; } }
Then install a plugin in the NuGet library to complete the connection to MySQL
Then add a controller
Public Iactionresult Index () { Lexancontext wordcontext = HttpContext.RequestServices.GetService (typeof ( AspNetCoreUseMySQL.Model.LexanContext) as Lexancontext; Return View (Wordcontext. Getlexan ()); return View (); }
Then add an MVC view
Then add the following code, you can also make changes according to your own situation
@model ienumerable<aspnetcoreusemysql.model.lexan>@{ viewbag.title = "Lexan";}
Then modify the Startup class and add the following code
Services. ADD (New Servicedescriptor (typeof (Lexancontext), New Lexancontext (Configuration.getconnectionstring (" DefaultConnection ")));
Then write the connection string to the Appsettings.json, notice here, everyone's password is different, you have to make a slight change, otherwise there will be a connection error
Let's create a database, create a table, and write a piece of data inside
Create database Aspnetcoreusemysql; Use Aspnetcoreusemysql; Show tables; CREATE TABLE Lexan (name varchar), sex char (1), age char (5)); Describe Lexan; INSERT into Lexan values (' Lexan ', ' 0 ', ' n '); SELECT * from Lexan;
All the work is done, let's run a look at the effect
Finally Bo Master here to say, any blogger blog, the trouble to add the original address, thank you! All blog posts come from Lexan's blog, and you can also focus on Blogger's post address http://www.cnblogs.com/R00R/
Aspnetcore using MySQL