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
18
votes
2 answers

Is AsList() better than ToList() with IDbConnection.Query() which returns IEnumerable?

I read this answer from Marc Gravell (@MarcGravell): https://stackoverflow.com/a/47790712/5779732 The last line says: As a minor optimization to your code: prefer AsList() to ToList() to avoid creating a copy. That statement is about…
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
18
votes
1 answer

How do I insert data when the primary key column is not an identity column?

I'm trying to insert data using Dapper.Contrib, in a table where the primary key column is not an identity column. The database table is created with this script: begin transaction create table dbo.Foos ( Id int not null, …
user247702
  • 23,641
  • 15
  • 110
  • 157
18
votes
1 answer

Dapper.net "where ... in" query doesn't work with PostgreSQL

The following query always produces the error "42601: syntax error at or near "$1" ". connection.Query( @"select manufacturer, model, year, AVG(price) as averageprice, AVG(miles) as averagemiles, COUNT(*) as…
sjdweb
  • 342
  • 2
  • 10
18
votes
4 answers

Dapper enum mapping

I need help for mapping enums with Dapper and Oracle. I have a field in Oracle type NUMBER (1) and must turn into an enum in my entity. public Status Status { get; set; } Status is a enum: public enum Status { [Description("Inactive", "0")] …
Marcelo Dias
  • 181
  • 1
  • 1
  • 3
18
votes
6 answers

Dapper Dynamic Parameters with Table Valued Parameters

I was trying to create a generic method, which can read the parameters name and value from a class at Runtime and create parameter collection for Dapper query execution. Realized that till the point all parameters are Input type it works well, but…
Mrinal Kamboj
  • 11,300
  • 5
  • 40
  • 74
18
votes
2 answers

Using Dapper QueryAsync to return a single object

Unfortunately, our DB is dated back to the 90s. Its legacy is so strong that we are still using SP in order to do most of the CRUD operations. However, it seems that Dapper suits pretty well and we have just started to play with it. However, I'm a…
Stefano Magistri
  • 1,130
  • 1
  • 11
  • 18
18
votes
2 answers

Dapper unlimited multi-mapping

So I have a situation where I have to join (and map) more than 7 entities (which as far as I see is the current limitation of Dapper). This is what I've got so far (pseudo code): using (var connection = new SqlConnection(_connectionString)) { …
Dimitar Dimitrov
  • 14,868
  • 8
  • 51
  • 79
18
votes
3 answers

Dapper - how to work with dynamic objects

I'm using Dapper to query from SQL and have a dynamic query as such: var returns = conn.Query(dynamicQuery); When I then cycle through the results, I would like to find out what the type of date I am handling is so I tried doing the…
cogumel0
  • 2,430
  • 5
  • 29
  • 45
17
votes
3 answers

Store enum as string in database

I am experimenting with dapper. I have a class which has an enum and the values are stored as strings in the database. This works with FluentNHibernate using GenericEnumMapper Is it possible to do the same with Dapper?
Yavor Shahpasov
  • 1,453
  • 1
  • 12
  • 19
17
votes
1 answer

How do I select an aggregate object efficiently using Dapper?

Lets say that I have a series of objects that form an aggregate. public class C{ public string Details {get;set;} } public class B{ public string Details {get;set;} public List Items {get;set;} } public class A{ public long ID…
Peter
  • 7,792
  • 9
  • 63
  • 94
17
votes
2 answers

Can't connect to SQL 2008 database using .NET Core 2.0

UPDATE I could never make this work with a "Windows Authentication" (domain) user. But with a "SQL Server Authentication" user everything is working like it's supposed to. ORIGINAL QUESTION My connectionString: Server=ip;Database=dbname;User…
tedi
  • 6,350
  • 5
  • 52
  • 67
17
votes
3 answers

Custom mapping in Dapper

I'm attempting to use a CTE with Dapper and multi-mapping to get paged results. I'm hitting an inconvenience with duplicate columns; the CTE is preventing me from having to Name columns for example. I would like to map the following query onto the…
Ant Swift
  • 20,089
  • 10
  • 38
  • 55
17
votes
2 answers

Dapper use singular table name

I experimented with Dapper and Dapper.Contrib. I have the following class: public class Customer { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public DateTime DateOfBirth {…
trenki
  • 7,133
  • 7
  • 49
  • 61
17
votes
2 answers

Dapper: mapping hierarchy and single different property

I really love Dapper's simplicity and possibilities. I would like to use Dapper to solve common challenges I face on a day-to-day basis. These are described below. Here is my simple model. public class OrderItem { public long Id { get; set; } …
Maxim Zhukov
  • 10,060
  • 5
  • 44
  • 88
17
votes
6 answers

Dapper.NET Connection/Query Best Practice

So i've read a bunch of links/SO questions, but i still can't get a clear answer on this. When performing SQL queries with Dapper in an ASP.NET application, what is the best practice for opening/closing connections? Here's the pattern i'm currently…
RPM1984
  • 72,246
  • 58
  • 225
  • 350