Dapper How to use: Dapper-dot-net/tests/tests.cs parsing (1) method: Testmultimapwithconstructor

Source: Internet
Author: User

Sql:

SELECT *  from users; SELECT *  from posts; SELECT *  from Posts P  Left JOIN  on u.id=  p.owneridORDER by p.id

Result set:

 Code:
String sql=@"Select *  fromPosts P Left JoinUsers u onU.id=P.owneridOrder  byp.id ";
Postwithconstructor[]Data=Connection. Query<Postwithconstructor,userwithconstructor,postwithconstructor>(SQL, (post,User)={Post. Owner= User;returnPost;}). ToArray ();

Analytical:method signature for query:
Publicstaticienumerable<treturn>query<tfirst,tsecond,treturn>(#ifCSHARP30thisidbconnection CNN,stringSql,func<tfirst,tsecond,treturn>map,ObjectParam,idbtransaction Transaction,BOOLBufferedstringSplitOn,int? Commandtimeout,commandtype?CommandType#elsethisidbconnection CNN,stringSql,func<tfirst,tsecond,treturn>map,Objectparam =NULL, idbtransaction transaction =NULL,BOOLBuffered =true,stringSplitOn ="Id",int? CommandTimeout =NULL, CommandType? CommandType =NULL#endif)

IDbConnectionextension methods, generics:<TFirst,Tsecond,Treturn>incoming Sql,func delegate:Func<TFirst,Tsecond,Treturn>in the instance code:
    1. (post, user)=>{ post.Owner= user;return post;}
This means that the return type object is derived from the first and second types. In this exampleTFirst Postwithconstructor,tsecond->Userwithconstructor,treturn->Postwithconstructorlook at the results:Note theTFirst,Tsecondthe order of the SQL tables, with the right link:the order of the tables and the order in which they are passed is important;Here's a question: How do you map the field of the result set and the attribute names of the entities are not the same? How do I handle a property with the same name? we notice that the returned result set has a rename field, and that the rename segment ID corresponds to a different table, with SELECT * so that the fields of the different tables can be taken out. Also note that the fields in the database table and the attribute names in the entity model are not consistent, actually they are based on the model entity's constructor to differentiate, we can see that the two model has a constructor:dapper with the result set column masterpiece, How to distinguish which columns are a table? Dapper the Division field, according to the division of the field name to split the column, the default partition field named: Id, can be specified manually, for this reason there is another rule: the Split field must be the first field of the table. For the following table design will be error. Dapper can now divide the result set 5 columns into two tables, which is the model to see the incomingTFirst,Tsecondthe order. Look at that .Postwithconstructorthe Owerid parameter of the constructor, which is not used in the constructor, but it is also indicated because it is distinguished from the Split field "Id" toPostwithconstructorentity these three: id,ownerid,[content] fields. So there has to be a match for these three fields in the constructor. That:System.Int32 Id, System.Int32 ownerid, System.String Content, case insensitive.

Dapper How to use: Dapper-dot-net/tests/tests.cs parsing (1) method: Testmultimapwithconstructor

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.