[Original].netcore through Petapoco operation MySQL, SQL Server, etc.

Source: Internet
Author: User

first, the introduction of Petapoco.netcore, Petapoco.netcore is based on the Petapoco light-weight orm, Support for. NETFramework and. Netcore, support for single Entity object mapping, multi-entity object mapping, Netcore No driver connection required, other APIs are consistent.

GitHub Address: Https://github.com/qingask/PetaPoco.NetCore

Nugut Address: Https://www.nuget.org/packages/PetaPoco.NetCore

Installing Pm>install-package Petapoco.netcore with NuGet

Ii.. Netcore Configuration
In Project.json add the corresponding. Netcore version of the database-driven reference, where MYSQL driver uses Pomelo.data.mysql,mysql official Netcore version drive compatibility too poor, pit too much, and so perfect can be replaced by the official Myssql Core Driver
"Dependencies": {
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc2-final",
"Microsoft.NETCore.App": {
"Version": "1.0.0-rc2-3002702",
"Type": "Platform"
},
"POMELO.DATA.MYSQL": "1.0.0",
"System.Text.Encoding.CodePages": "4.0.1"
}

Third, the use of Petapoco.netcore

1. Test SQL

Test sqlcreate Table Blogs (BlogId int (one) NOT null PRIMARY key,url varchar (n) DEFAULT NULL); CREATE table post (ID int,titl e varchar (+), author int);d ROP table Author;create table author (id int,name varchar); INSERT into blogs values (1, ' Test 1 '); INSERT INTO blogs values (2, ' test2 '); insert into author value (1, ' author 1 '); Insert into author value (2, ' author 2 '); INSERT into post values (1, ' Book1 ', 1); INSERT into post values (2, ' Book2 ', 1); INSERT into post values (3, ' Book3 ', 2); INSERT into post values (4, ' Book4 ', 2);

  

2. Using Petapoco.netcore

           Mysqlconnection connection = new Mysqlconnection ("" Server=localhost;database=test;uid=root;password=123456;char Set=utf8;            Sslmode=none "");            var db = new Database (connection);            Entity Test Blog blog = New Blog () {BlogId = 3, Url = "test3"}; Save var result = db.              Insert (blog); Edit Blog.            URL = "test333"; result = db.               Update (blog); Delete result = db.            Delete (blog);            SQL Test var sql1 = Sql.Builder.Append ("INSERT into blogs values (4, ' test4 ')"); result = db.            Execute (SQL1);            var sql2 = Sql.Builder.Append ("Update blogs set url= ' test444 ' where blogid=4 '); result = db.            Execute (SQL2); query var model2 = db.              Singleordefault<blog> (1); Listing var list = db. Query<blog> (Sql.Builder.Append ("SELECT * from Blogs")).              ToList (); paging var list2 = db. Page<blog> (1, 2, Sql.Builder.Append ("Select * from Blogs"));            query var sql3 = Sql.Builder.Append ("select * FROM blogs where blogid=4"); var model1 = db. Query<blog> (SQL3).            FirstOrDefault (); var model3 = db.            Firstordefault<blog> (SQL3); Returns multiple results test result = db.                Fetch<post, author, post> (p, a) = = {P.author_obj = A;            return p;            }, @ "SELECT * from post left JOIN author to post.author = Author.id ORDER by post.id"); using (var multi = db. Querymultiple ("SELECT * from Post")) {result = multi. Read<post> ().            ToList (); } using (var multi = db.                Querymultiple (@ "SELECT * from post left JOIN author in post.author = Author.id ORDER by post.id")) { result = multi. Read<post, author, post> ((p, a) = = {P.author_obj = A; return p;}).            ToList (); } USINg (var multi = db.            Querymultiple ("SELECT * from Post;select * from author;")) {var p = multi. Read<post> ().                First (); var a = multi. Read<author> ().            First (); }

  

[Original].netcore through Petapoco operation MySQL, SQL Server, etc.

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.