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:
- Object mapping can be done automatically using Dapper!
- Lightweight, single file.
- Supports multiple databases.
- 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)
//Book Public classBook { PublicBook () {Reviews=NewList<bookreview>(); } Public intId {Get;Set; } Public stringName {Get;Set; } Public VirtualList<bookreview> Reviews {Get;Set; } Public Override stringToString () {return string. Format ("[{0}]------' {1} '", Id, Name); } }//Book review Public classBookreview { Public intId {Get;Set; } Public intBookId {Get;Set; } Public Virtual stringContent {Get;Set; } Public VirtualBook Assoicationwithbook {Get;Set; } Public Override stringToString () {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.
New SqlConnection (connstring);
Insert
New Book (); Name="C # essentialism "string"INSERT into book (Name) VALUES (@ Name)"; // to manipulate an object Conn. Execute (query, book); // Direct Assignment Operation New " C # Essence theory "});
Update
string " UPDATE book SET [Email protected] WHERE ID [email protected]"; Conn. Execute (query, book);
Delete
string " DELETE from book WHERE id = @id " new {id = id});
Query
string " SELECT * from book " ; // no parameter query, return list, with parameter query and previous parameter assignment method is the same. Conn. query<book>// returns a single message string"select * FROM book WHERE id = @ ID"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 operationsstringquery ="SELECT * from book B left joins Bookreview BR on Br. BookId = b.id WHERE b.id = @id"; Book Lookup=NULL;//Query<tfirst, Tsecond, treturn> varb = Conn. Query<book, Bookreview, book>(Query, (book, Bookreview)={//scan the first record to determine non-null and non-repetition if(Lookup = =NULL|| Lookup. 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); returnLookup; }, New{id =ID}). Distinct (). Singleordefault ();returnb
//1--1 Operationbookreview BR;stringquery ="SELECT * from Bookreview WHERE id = @id";using(conn) {BR= Conn. Query<bookreview, book, bookreview>(query, (bookreview, book)={Bookreview.assoicationwithbook=Book ; returnBookreview; }, New{id =ID}). Singleordefault (); returnBR;}
using(conn) {//Start a transactionIDbTransaction transaction =Conn. BeginTransaction (); Try{stringquery ="DELETE from book WHERE id = @id"; stringQuery2 ="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 a transaction Transaction.commit (); }Catch(Exception ex) {//exception occurred, transaction rollback transaction. Rollback (); Throw NewException (ex. Message); }
}
Basic use of Dapper