. 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:
CREATE DATABASE' Test 'CREATE TABLE`User' (' Id 'int( One) not NULLauto_increment, ' UserName 'varchar(255)DEFAULT NULL, ' Url 'varchar(255)DEFAULT NULL, ' age 'int( One)DEFAULT NULL, PRIMARY KEY(' Id ')) ENGINE=InnoDBDEFAULTCHARSET=Gbk
Create a new user class
Public classUser { Public intId {Get;Set; } Public stringUserName {Get;Set; } Public stringURL {Get;Set; } Public intAge {Get;Set; } }
Below to operate MySQL additions and deletions to change
Public Static voidMain (string[] args) {Encoding.registerprovider (codepagesencodingprovider.instance); Mysqlconnection Con=NewMysqlconnection ("server=127.0.0.1;database=test;uid=root;pwd=;charset= ' GBK '"); //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 (); }
Easy to use dapper, more versatility to view official documents.
Execution effect:
Github:https://github.com/linezero/blog/tree/master/netcoremysql
If you think this article is helpful to you, please click " recommend ", thank you.
. NET Core uses dapper to operate MySQL