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.
Currently EF Core does not support MySQL, this article mainly explains the use of dapper operation MySQL.
Third party MySQL Connector:https://github.com/sapientguardian/mysql-connector-net-netstandard
Dapper:https://github.com/stackexchange/dapper-dot-net
New Project
Create a new. NET Core Console Application Netcoremysql
Add Reference
Using the NuGet console to add
Install-package SapientGuardian.MySql.Data-preinstall-package dapper-pre
MySQL additions and deletions to check changes
Create a new test library and table in MySQL
Script used for testing:
CREATEDATABASE' Test 'CREATETABLE 'User' (' Id 'int11)NotNullAuto_increment, ' UserName 'varchar (255) default null, ' URL ' varchar (255) default null, ' age ' int ( One ) default NULL, PRIMARY KEY (' Id ')) ENGINE=innodb default CHARSET= GBK;
Create a new user class
public class User { public int Id {get; set;} public string UserName { Get set;} public string Url {get; Span style= "COLOR: #0000ff" >set;} public int Age {get; set
Below to operate MySQL additions and deletions to change
PublicStaticvoid Main (String[] args) {encoding.registerprovider (codepagesencodingprovider.instance); Mysqlconnection con =New Mysqlconnection ("server=127.0.0.1;database=test;uid=root;pwd=;charset= ' GBK '");//New Data con. Execute ("Insert into user values (null, ' Test ', ' http://www.cnblogs.com/linezero/', 18)");//New data return self-increment IDvar id=con. queryfirst<Int> ("Insert into user values (null, ' Linezero ', ' http://www.cnblogs.com/linezero/'); select last_insert_id ();");//Modify Data con. Execute ("Update user Set UserName = ' linezero123 ' where Id = @Id",New {Id =ID});//Querying datavar list=con. Query<user> ("SELECT * FROM user");foreach (VAR itemInchList) {Console.WriteLine ($"User name: {item. UserName} Link: {Item. URL}"); }//Delete Data con. Execute ( "delete from user where Id = @Id ", new {Id = ID}); Console.WriteLine ( " results after deleting data "); List = con. Query<user> ( "select * from User" ); foreach (var item in list) {Console.WriteLine ($ " user name: {Item. UserName} Link: {Item. Url} ");} Console.readkey (); }
Easy to use dapper, more versatility to view official documents.
Execution effect:
Github:https://github.com/linezero/blog/tree/master/netcoremysql
Reprinted from Http://www.cnblogs.com/linezero/p/NETCoreMySQL.html
. NET Core uses dapper to operate MySQL