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
85
votes
9 answers

Multi-Mapper to create object hierarchy

I've been playing around with this for a bit, because it seems like it feels a lot like the documented posts/users example, but its slightly different and isn't working for me. Assuming the following simplified setup (a contact has multiple phone…
Jorin
  • 1,652
  • 1
  • 19
  • 25
82
votes
4 answers

Does Dapper support SQL 2008 Table-Valued Parameters?

Does anyone know if is possible to pass table-valued parameter data to a stored procedure with Dapper?
Carlos Mendes
  • 1,900
  • 1
  • 18
  • 33
81
votes
8 answers

How to map to a Dictionary object from database results using Dapper Dot Net?

If I have a simple query such as: string sql = "SELECT UniqueString, ID FROM Table"; and I want to map it to a dictionary object such as: Dictionary myDictionary = new Dictionary(); How would I do this with…
jpshook
  • 4,834
  • 6
  • 36
  • 45
79
votes
3 answers

Dapper.Rainbow VS Dapper.Contrib

Can someone please explain the difference between Dapper.Rainbow vs. Dapper.Contrib? I mean when do you use SqlMapperExtensions.cs of Dapper.Contrib and when should you use Dapper.Rainbow?
Shuaib
  • 1,561
  • 3
  • 19
  • 28
76
votes
7 answers

How to implement Unit Of Work pattern with Dapper?

Currently, I am trying to use Dapper ORM with Unit Of Work + Repository Pattern. I want to use Unit of Work as opposed to a simple dapper Repository due to the fact that my insert and updates require a degree of transaction processing. I have been…
Stig
  • 1,169
  • 1
  • 9
  • 12
76
votes
3 answers

Passing Output parameters to stored procedure using dapper in c# code

I have a stored procedure in this format CREATE PROCEDURE SP_MYTESTpROC @VAR1 VARCHAR(10), @VAR2 VARCHAR(20), @BASEID INT , @NEWID INT OUTPUT As Begin INSERT INTO TABLE_NAME(username, firstname) select @VAR1, @VAR2 …
lacoder
  • 1,253
  • 1
  • 11
  • 22
76
votes
6 answers

Bulk inserts taking longer than expected using Dapper

After reading this article I decided to take a closer look at the way I was using Dapper. I ran this code on an empty database var members = new List(); for (int i = 0; i < 50000; i++) { members.Add(new Member() { Username =…
kenwarner
  • 28,650
  • 28
  • 130
  • 173
72
votes
4 answers

Get DateTime as UTC with Dapper

I'm using Dapper to map my entities to SQL Server CE. If I save a DateTime with Kind=Utc, when I read it back I get a DateTime with Kind=Unspecified, which leads to all kind of problems. Example: var f = new Foo { Id = 42, ModificationDate =…
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
62
votes
4 answers

CancellationToken with async Dapper methods?

I'm using Dapper 1.31 from Nuget. I have this very simple code snippet, string connString = ""; string query = ""; int val = 0; CancellationTokenSource tokenSource = new CancellationTokenSource(); using (IDbConnection conn = new…
Pedigree
  • 2,384
  • 3
  • 23
  • 28
61
votes
9 answers

Dapper. Paging

I am trying Dapper ORM and I am querying a a Posts table. But I would like to get paged results ... 1 - How can I do this? Isn't there a helper for this? 2 - Can Dapper Query return an IQueryable? Thank You, Miguel
Miguel Moura
  • 36,732
  • 85
  • 259
  • 481
60
votes
2 answers

Why does Dapper's .Execute(...) return an int?

Anyone know why Dapper returns an int from .Execute(...) ? I can't find this documented anywhere.
Drew R
  • 2,988
  • 3
  • 19
  • 27
60
votes
2 answers

Multiple SQL statements in one roundtrip using Dapper.NET

There is a nice feature in ADO.NET that allows you to send multiple SQL statements to database in one roundtrip and receive results for all statements: var command = new SqlCommand("SELECT count(*) FROM TableA; SELECT count(*) FROM TableB;",…
user1224129
  • 2,759
  • 3
  • 27
  • 29
59
votes
6 answers

Is there any way to trace\log the sql using Dapper?

Is there a way to dump the generated sql to the Debug log or something? I'm using it in a winforms solution so the mini-profiler idea won't work for me.
Mladen Mihajlovic
  • 6,095
  • 7
  • 40
  • 55
53
votes
3 answers

What does the buffered parameter do in Dapper dot net?

Dapper dot net has a buffer parameter (a bool), but as far as I can tell the only thing it does is cast the result to a list before returning it. As per the documentation: Dapper's default behavior is to execute your sql and buffer the entire …
smdrager
  • 7,327
  • 6
  • 39
  • 49
51
votes
1 answer

Dapper & TransactionScope?

I just started playing around with Dapper. So far i love it. Does dapper not work with TransactionScope? I noticed that even if i never call TransactionScope.Complete then my changes are still committed to the database. If TransactionScope isn't…
coding4fun
  • 8,038
  • 13
  • 58
  • 85