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)

//BookPublicClass Book{  Public book () {  Reviews =New List<bookreview> (); }  Publicint Id {GetSet }  PublicString Name {GetSet }  PublicVirtual List<bookreview> Reviews {GetSet }  PublicOverrideString ToString () {   ReturnString. Format ("[{0}]------{1} ", Id, Name); }}//ReviewPublicClass Bookreview{  Publicint Id {GetSet }  Publicint BookId {GetSet }  Publicvirtual string Content {get; set;}   public virtual book Assoicationwithbook {get; set;}   public override string ToString ()   {   return string. Format ( "{0}"--[{1}]\t\ "{3}\" ", Id, BookId, Content); }}           /span>                
    • 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.

New SqlConnection (connstring);

Insert

new book (); Book. Name= "INSERT into book(Name) VALUES (@name)";  manipulate the object conn. Execute (query, book); //"C # Essence"});       

Update

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

Delete

string query = "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. Implementation of 1--N query operationsstring 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 to determine non-null and non-repeating if (lookup = = null | | lookup. Id! = Book.    ID) lookup = Book;    // book The corresponding review is not empty, add the current book review list, and finally remove the duplicate book. if (bookreview! = null) lookup.     Reviews.add (Bookreview);  return lookup; }, new {id = id}). Distinct (). Singleordefault (); return b;             
1--1 operation Bookreview BR; "select * from bookreview WHERE id = @id";  using (conn) {BR = conn.    Query<bookreview, book, bookreview> (query, [Bookreview, Book] = {bookreview.assoicationwithbook = book; 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 FORM bookreview WHERE BookId =    @BookId "; Conn. Execute (Query2, new {BookId = id}, transaction, null, null); Conn. Execute (query, new {id = id}, transaction, null,     NULL);  // COMMIT transaction transaction.commit (); } catch (Exception ex) {// exception, transaction rollback TRANS Action.    Rollback (); throw new Exception (ex.  Message); }} 

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.