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
51
votes
7 answers

Can I map a result to Tuple in Dapper?

I am trying to select a list of two integer columns map the results to a Tuple. For example: connection.Query>("select id1, id2 from sometable").ToList(); does not work, but the same query does work if I create a class with…
Tom Gerken
  • 2,930
  • 3
  • 24
  • 28
48
votes
4 answers

Getting "The connection does not support MultipleActiveResultSets" in a ForEach with async-await

I have the following code using Dapper.SimpleCRUD : var test = new FallEnvironmentalCondition[] { new FallEnvironmentalCondition {Id=40,FallId=3,EnvironmentalConditionId=1}, new FallEnvironmentalCondition…
MotKohn
  • 3,485
  • 1
  • 24
  • 41
48
votes
2 answers

Dapper and SQL Injections

How does Dapper help protect against SQL injections? I am testing out different DAL technologies and have to choose one to be secure our site. I'm leaning towards Dapper (http://code.google.com/p/dapper-dot-net/), but need some help learning about…
cdub
  • 24,555
  • 57
  • 174
  • 303
48
votes
1 answer

Explanation of dapper buffer/cache

I use dapper to return objects from my database as IEnumerable. As default dapper has buffer setting set to true. How does this work? If dapper cache the first query and then get the objects from memory. What happens if someone edit/delete/add rows…
Nils Anders
  • 4,212
  • 5
  • 25
  • 38
47
votes
3 answers

How to return dynamic types List with Dapper ORM

I have been using Dapper.net for a while now and its a very good ORM mapper which works great with .Net dynamic types. But I noticed that when Dapper retrieves data from a database it returns as DapperRow type. Is there are any way that I can return…
Husni Jabir
  • 545
  • 1
  • 6
  • 17
46
votes
1 answer

How to pass a null parameter with Dapper

I have a stored procedure that has a parameter with no default value, but it can be null. But I can't figure out how to pass null with Dapper. I can do it just fine in ADO. connection.Execute("spLMS_UpdateLMSLCarrier", new { **RouteId =…
user1466918
44
votes
6 answers

How do I build a dynamic Where clause with Dapper when passing in a model

I have an example model that looks like this: public class PersonModel { public int Id {get; set;} public string FirstName {get; set;} public string Lastname {get; set;} public string City {get; set;} } In my repository I want…
Eldorian
  • 603
  • 1
  • 6
  • 18
44
votes
1 answer

Latest Dapper VS Entity Framework 6 performance considerations

There are a few performance comparisons between Dapper (which seems to be the fastest, most popular "micro ORM tool"). Now it's September 2014 and we have Entity Framework 6 (not 5, or 4) and Dapper is still around. We will begin developing a huge…
John
  • 3,591
  • 8
  • 44
  • 72
43
votes
1 answer

Simple inner join result with Dapper?

I can't seem to find documentation or examples for my problem (been searching a while now). I think my problem is pretty straightforward, so here goes. I have two tables. My primary table is called Persons and the secondary table is PersonEntries.…
Per Hornshøj-Schierbeck
  • 15,097
  • 21
  • 80
  • 101
42
votes
3 answers

When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id", "splitOn

I'm trying to use the Multi-mapping feature of dapper to return a list of Album and associated Artist and Genre. public class Artist { public virtual int ArtistId { get; set; } public virtual string Name { get; set; } } public class…
user2497013
  • 421
  • 1
  • 4
  • 4
39
votes
5 answers

Dapper and anonymous Types

Is it possible to use anonymous types with Dapper? I can see how you can use dynamic i.e. connection.Query(blah, blah, blah) is it then possible to do a .Select(p=> new { A, B ,C }) or some variation of that afterwards? Edit I thought…
Peter
  • 7,792
  • 9
  • 63
  • 94
37
votes
4 answers

Why Entity Framework performs faster than Dapper in direct select statement

I'm new to using ORM in dealing with database, Currently I'm making a new project and I have to decide if i'll use Entity Framework or Dapper. I read many articles which says that Dapper is faster than Entity Framework. So I made 2 simple prototype…
Ahmed
  • 559
  • 1
  • 5
  • 14
35
votes
9 answers

Check if record exists with Dapper ORM

What is the simplest way to check if record exists using the Dapper ORM? Do I really need to define POCO objects for a query where I only want to check if a record exists?
webworm
  • 10,587
  • 33
  • 120
  • 217
35
votes
1 answer

How does Dapper compare to ADO.NET?

When should Dapper be used instead of ADO.NET? I would like to understand the pros and cons of Dapper over ADO.NET. What are the advantages of Dapper that would motivate its use?
Divya
  • 365
  • 1
  • 4
  • 4
35
votes
2 answers

Dapper Parameter replace not working for Top

This is my sql var maxLimit =100; var sql = "Select Top @MaxLimit from Table WHere data =@Id" conn.Query(sql, new { Id = customerId, MaxLimit = maxLimit }) But I get a system error incorrect…
Justin Homes
  • 3,739
  • 9
  • 49
  • 78