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)
//BookPublicClassBook {PublicBook () {reviews =New list<bookreview>(); }Publicint Id {GetSet; }PublicString Name {GetSet; }PublicVirtual List<bookreview> Reviews {GetSet; }PublicOverrideStringToString () {ReturnString. Format ("[{0}]------' {1} '", Id, Name); } }//ReviewPublicClassBookreview {Publicint Id {GetSet; }Publicint BookId {GetSet;} public virtual string Content {get; set;} public virtual book Assoicationwithbook {get; set;} public override string< Span style= "COLOR: #000000" > 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.
New SqlConnection (connstring);
Insert
New book (); Name="C # Essence ""INSERT into book (Name) VALUES (@name)"; manipulate the object Conn. Execute (query, book); //"C # Essence "});
Update
"UPDATE book SET [email protected] WHERE ID [email protected]"; Conn. Execute (query, book);
Delete
"DELETE from book WHERE id = @id"new {id = id});
Query
"select * from book"; no parameter query, return list, with parameter query and previous parameter assignment method is the same. Conn. Query<book>//"select * FROM book WHERE id = @id"
- 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 joins 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 book review is not empty, add the current book review list, and finally remove the duplicate book. if (bookreview! = nullreturn 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> {bookreview.assoicationwithbook = book; return Bookreview; }, new {ID = ID}). Singleordefault (); return BR;}
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); // Submit Transaction Transaction.commit (); } catch (Exception ex) { // exception occurred, transaction rollback transaction. Rollback (); throw New Exception (ex. Message); }}
Basic use of dapper