Basic use of Dapper

Source: Internet
Author: User

Dapper is the. NET next micro ORM, which is lightweight and semi-automatic, unlike the Entity Framework or nhibnate. This means that the entity classes are written by themselves. It does not have a complex configuration file, a single file can be. Give the official address.

http://code.google.com/p/dapper-dot-net/

He feels that he is very useful and has now replaced the original SqlHelper. Advantages:

    1. Object mapping can be done automatically using Dapper!
    2. Lightweight, single file.
    3. Supports multiple databases.
    4. The dapper principle is to get and produce objects quickly by emit the sequence queue that reflects IDataReader.

There are extended classes on the internet for dapper, which is not to be discussed here. The following is only the simple additions and deletions, database tables and the corresponding relationship between the application of the transaction.

First give the relationship of the entity class:

Books and book reviews are 1---n relationships. (The entity class that inherits the entities framework, virtual indicates lazy loading, ignored here)

The public class book    {public Books        ()        {            reviews = new list<bookreview> ();        }        public int Id {get; set;}        public string Name {get; set;}        Public virtual list<bookreview> Reviews {get; set;}        public override string ToString ()        {            return string. Format ("[{0}]------" {1} ", Id, Name);}    } Review public class Bookreview    {public        int Id {get; set;}        public int BookId {get; set;}        Public virtual string Content {get; set;}        Public virtual book Assoicationwithbook {get; set;}        public override string ToString ()        {            return string. Format ("{0})--[{1}]\t\" {3}\ "", Id, BookId, Content);        }    }
    • Basic additions and deletions to check the operation

Since the operation of the dapper Orm is actually an extension of the IDbConnection class, all methods are extension methods of that class. So instantiate a IDbConnection object before using it.

IDbConnection conn = new SqlConnection (connstring);

Insert

Book book = new book (); Book. Name= "C # Essence Theory"; string query = "INSERT into book (Name) VALUES (@name)";//Manipulate the object conn. Execute (query, book);//Direct Assignment Operation Conn. Execute (query, new {name = "C # essence"});

Update

string query = "UPDATE book SET  [email protected] WHERE ID [email protected]"; Conn. Execute (query, book);

Delete

string query = "DELETE from book WHERE id = @id"; Conn. Execute (query, book); Conn. Execute (query, new {id = id});

Query

string query = "SELECT * from book";//no parameter query, return list, with parameter query and previous parameter assignment method is the same. Conn. Query<book> (query). ToList (); Returns a single message string query = "SELECT * FROM book WHERE id = @id"; Book = conn. Query<book> (query, new {id = id}). Singleordefault ();     
    • database table Correspondence Relation operation
When querying a book, find the corresponding book review at the same time, and there is a list. Implement 1--N query operation string query = "SELECT * from book B left JOIN bookreview BR on Br. BookId = b.id WHERE b.id = @id "; Book lookup = Null;//query<tfirst, Tsecond, treturn> var b = conn. Query<book, Bookreview, book> (query, book, Bookreview) =//scan the first record, judging non-null and non-repeating if (lookup = = NULL | | lo Okup. Id! = Book.    ID) lookup = Book;    Books corresponding to the book review is not empty, added to the current book review list, and finally the repetition of the book removed. if (Bookreview! = null) lookup.     Reviews.add (Bookreview);  return lookup; }, new {id = id}). Distinct (). Singleordefault (); return b;
1--1 operation bookreview br;string query = "SELECT * from Bookreview WHERE id = @id"; using (conn) {BR = conn.    Query<bookreview, book, bookreview> (query, [Bookreview, Book] = {bookreview.assoicationwithbook = book;   return bookreview; }, new {id = id}).  Singleordefault (); return BR;}
    • Transactional operations
using (conn) {//start transaction idbtransaction TRANSACTION = conn.  BeginTransaction ();    try {string query = "DELETE from book WHERE id = @id";    String Query2 = "DELETE from Bookreview WHERE BookId = @BookId"; Conn.    Execute (Query2, new {BookId = id}, transaction, NULL, NULL); Conn.    Execute (query, new {id = id}, transaction, NULL, or NULL);  Commit Transaction Transaction.commit (); } catch (Exception ex) {//exception occurred, transaction ROLLBACK TRANSACTION.    Rollback (); throw new Exception (ex.  Message); }}

Article source https://www.cnblogs.com/Sinte-Beuve/p/4231053.html

Basic use of Dapper

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.