Any good samples for getting started with Dapper? [closed]

扶醉桌前 提交于 2019-11-29 22:55:44
Marc Gravell

That is, in part, because there is nothing to set up - all you need is a database (which it doesn't care about) and some classes (which it doesn't care about).

The core methods just take parameterised SQL, and are deliberately close to LINQ-to-SQL's sql-based methods (hint: we use dapper as a direct drop-in replacement whenever we get issues with LINQ-to-SQL).

If you want a few examples, the "tests" project contains examples of the core APIs.

If you mean "how do I add dapper" - two choices; a single file added to your project, or a nuget package. The nuget pacakge tends to lag a little bit, but not much.

But ultimately, usage is just:

// get all open orders for this customer
var orders = connection.Query<Order>(
    "select * from Orders where CustomerId = @custId and Status = 'Open'",
    new { custId = customerId }).ToList();

where your Orders class has properties with names matching the database (it is a very direct map). No attributes are required; no special tooling is required. In our case, we tend to use LINQ-to-SQL generated classes with it, or a specific class created for some subset of columns (or composite between several tables, etc).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!