Questions tagged [dapper]

Dapper is a micro-ORM for .NET developed and used by the Stack Overflow team, focusing on raw performance as the primary aim.

Dapper is a micro-ORM, offering core parameterization and materialization services, but (by design) not the full breadth of services that you might expect in a full ORM such as LINQ-to-SQL or Entity Framework. Instead, it focuses on making the materialization as fast as possible, with no overheads from things like identity managers - just "run this query and give me the (typed) data".

However, it retains support for materializing non-trivial data structures, with both horizontal (joined data in a single grid) and vertical (multiple grids) processing.

Quite possibly the fastest materializer available for .NET, and available here: github.com/StackExchange/dapper-dot-net

Getting Started with Dapper

Dapper comprises of a single file: SqlMapper.cs (or SqlMapperAsync.cs for .NET 4.5 if you like async). You can either include the file, as is, in your project. Or install it via nuget.

Here is a video with a simple example.

A simple Hello World sample

using Dapper;
//...
using (var connection = new SqlConnection(myConnectionString))
{
  connection.Open();
  var posts = connection.Query<Post>("select * from Posts");

  //... do stuff with posts
}

CRUD operations

Dapper provides a minimal interface between your database and your application. Even though the interface is minimal it still supports the full array of database operations. Create, Read, Update, Delete are fully supported. Using stored procedures or not.

See also:

The Multi Mapper

Dapper allows you to automatically split a single row to multiple objects. This comes in handy when you need to join tables.

See also:

Performance

A key feature of Dapper is performance. You can go through the performance of SELECT mapping over 500 iterations at this link.

.NET Framework support

Dapper runs best on .NET 4.0 or later. It makes use of Dynamic features in the language and optional params. You can run Dapper on .NET 3.5 as well. There are no known ports that avoid dynamic method generation. There are no known ports to .NET 2.0.

Dapper also works fine in Mono.

Nuget package

Dapper can most easily be installed through its NuGet package.

Install-Package Dapper

Install

Dapper can be used after adding the Reference of of Dapper.dll to the project

Extensions

The Dapper solution includes the following extensions:

  • Dapper.Rainbow
  • Dapper.Contrib
  • Dapper.SqlBuilder

3rd party extensions

There are some extensions available, developed by 3rd parties:

2873 questions
17
votes
5 answers

Execute stored procedure w/parameters in Dapper

I'm using Dapper (thanks Sam, great project.) a micro ORM with a DAL and by some reason I'm not able to execute stored procedures with input parameters. In a example service I've the following code: public void GetSomething(int somethingId) { …
antao
  • 767
  • 1
  • 9
  • 24
17
votes
2 answers

Using Dapper-dot-net, how do I map SQL uniqueidentifier column to a .Net type?

I have an existing database that uses the SQL "uniqueidentifier" type, which looks like a different GUID format than a .Net GUID. Question: Using Dapper, how do I map SQL uniqueidentifier column to .Net? Issue: Using Dapper-dot-net, when I map the…
Dan Sorensen
  • 11,403
  • 19
  • 67
  • 100
17
votes
1 answer

Dapper and In Condition

Using Dapper, the following throws Incorrect syntax near ','. const string sql = "select * from ZipToZipDistance z where z.NoRouteFound = 0" + " and z.OriginZip in (@zips) or z.DestZip in (@zips)"; var zipStrings = zips.Select(x =>…
Tim Scott
  • 15,106
  • 9
  • 65
  • 79
16
votes
3 answers

Correct method of deleting over 2100 rows (by ID) with Dapper

I am trying to use Dapper support my data access for my server app. My server app has another application that drops records into my database at a rate of 400 per minute. My app pulls them out in batches, processes them, and then deletes them from…
Vaccano
  • 78,325
  • 149
  • 468
  • 850
16
votes
2 answers

Can Dapper return values from a SQL function?

I have a SQL function that returns an INT, when I try to call it via dapper I always get no results back. I'm calling it like this: var result = _connection.Query("functionname", new {Parm = 123}, commandType:…
ilivewithian
  • 19,476
  • 19
  • 103
  • 165
16
votes
3 answers

How to create an anonymous object with property names determined dynamically?

Given an array of values, I would like to create an anonymous object with properties based on these values. The property names would be simply "pN" where N is the index of the value in the array. For example, given object[] values = { 123, "foo"…
Jeff Ogata
  • 56,645
  • 19
  • 114
  • 127
16
votes
1 answer

How do I get dapper to map an int to a boolean property

I have this class... public class MyDTO { public int Id { get; set; } public bool Selected { get; set; } } and then use dapper to attempt to create a list like this... var list = this.db.OpenConnection().Query( …
Tim Bailey
  • 571
  • 1
  • 3
  • 15
16
votes
2 answers

Dapper ORM paging and sorting

I am giving the Dapper ORM a try. I am able to query data from a table using the code below: Dim comments As List(Of Comment) Using conn = New SqlConnection(ConnectionString) conn.Open() comments = conn.Query(Of Comment)("SELECT * from…
ericdc
  • 11,217
  • 4
  • 26
  • 34
16
votes
1 answer

What is the difference between ORM and micro ORM?

Please mention the difference between (big) ORM and micro ORM. What are the advantages of micro ORM over big ORM. For eg. the difference between entity framework ORM and dapper micro ORM.
Brillian
  • 1,411
  • 2
  • 16
  • 23
16
votes
3 answers

How do I use Dapper to get the return value of stored proc?

I'm using Dapper in asp.net mvc 4 project .net f/w 4.6.1 using sql server 2016 express I have a stored proc which deletes from 2 tables which should be…
Hamza Ahmed
  • 1,571
  • 4
  • 18
  • 35
16
votes
1 answer

Authentication and Authorization without Entity Framework in ASP.NET 5 MVC 6

I'm trying to configure my authentication and authorization using my existing database and tables, without using Entity Framework (using Dapper). I've got the Dapper configured correctly, now I'm trying to hook up the SignInManager, and UserManager…
ganders
  • 7,285
  • 17
  • 66
  • 114
16
votes
4 answers

How can I make Dapper.NET throw when result set has unmapped columns?

Using the example code below as context... When I run this query I get the 'Id' field coming back as default value (which is 0 for an int). I would like to tell dapper to run in a manner where it would throw an exception if there is a column in the…
wlscaudill
  • 505
  • 4
  • 6
16
votes
1 answer

How to get SqlDataReader with Dapper?

I have a web application that uses old school SqlHelper class. I want to create my custom SqlHelper which uses Dapper underneath. So, how can I get SqlDataReader from Dapper?
Imran Qadir Baksh - Baloch
  • 32,612
  • 68
  • 179
  • 322
16
votes
1 answer

Dapper multiple objects from one row

I have one row coming from the database select "John" Name, "Male" Gender, 20 Age, "Rex" PetName, "Male" PetGender, 5 PetAge // ... many more ... Using Dapper, I'd like to pull this row into two…
Jay Sullivan
  • 17,332
  • 11
  • 62
  • 86
16
votes
2 answers

Dapper to DataTable

I have a scenario where I need to return a DataTable from a query using Dapper. How do I return a DataTable from a query using Dapper? DataTable dt = connection.Query("SELECT * FROM table");
user2421145
  • 305
  • 1
  • 3
  • 9