. NET Core uses dapper to operate MySQL

Source: Internet
Author: User
Tags dot net

. 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

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.