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

How to use Dapper with Linq

I'm trying to convert from Entity Framework to Dapper to hopefully improve data access performance. The queries I use are in the form of predicates like so Expression>. To give an example: I have the following code which I need to…
Rian Mostert
  • 714
  • 1
  • 7
  • 19
20
votes
3 answers

How to retrieve a single value from the database using Dapper

I'm using Dapper and trying to retrieve a short from the database and would like to do this without grabbing it from a collection. I've got the following code, which doesn't work because QueryAsync returns IEnumerable. short…
Nic
  • 12,220
  • 20
  • 77
  • 105
20
votes
2 answers

Dapper: How to get value from DapperRow if column name is "count(*)"?

I have a dynamic result from Dapper query that contains records like this: {DapperRow, billing_currency_code = 'USD', count(*) = '6'} I'm able to access 'USD' by using rowVariable.billing_currency_code To get '6' value I tried…
Zelid
  • 6,905
  • 11
  • 52
  • 76
20
votes
4 answers

Query very fast but mapping slow with dapper

I'm using dapper for a new project and love it but I don't understand why my queries are really slow. The execution time is very fast, almost instant, but the connection stays open much longer while dapper is mapping the result to my object I…
Dawmz
  • 344
  • 1
  • 3
  • 6
20
votes
3 answers

Using Dapper QueryMultiple in Oracle

I´m trying to use dapper with Oracle (ODP.NET) and I would like to use the "QueryMultiple" functionality. Passing this string to the QueryMultiple method: var query = "Select CUST_ID CustId from Customer_info WHERE CUST_ID=:custId;" + …
Kamolas81
  • 539
  • 1
  • 3
  • 10
20
votes
4 answers

Dapper query with list of parameters

I am trying to run a query with Dapper with a known set of parameters, but with a list of values for those parameters. A simple example of what I am trying to do would be: DateTime endDate = DateTime.Now; DateTime startDate =…
Jarrod
  • 1,415
  • 2
  • 14
  • 22
20
votes
3 answers

Using Dapper to map more than 5 types

I am currently building a SELECT query that joins 12 tables together. I've been using Dapper for all my other queries and it works great. Problem is, the generic methods only have to five generic parameters. I've previously modified the code to…
Christian Droulers
  • 853
  • 1
  • 8
  • 21
19
votes
2 answers

How can i use dapper to connect to a sqlite database?

How can I use dapper to connect and get data from a sqlite database?
David
  • 5,403
  • 15
  • 42
  • 72
19
votes
3 answers

Dapper.net transaction problem

I'm trying to commit a transaction to my Sql Server 2008 database - firstly 2 insert's followed by a couple update's, however, as soon as it attempts to execute the first of the update's, I get the following error: ExecuteNonQuery requires the…
marcusstarnes
  • 6,393
  • 14
  • 65
  • 112
19
votes
2 answers

How to periodically flush dapper.net cache when used with SQL Server

Can someone please explain what this means (from the Dapper.net website) Limitations and caveats Dapper caches information about every query it runs, this allow it to materialize objects quickly and process parameters quickly. The current…
Gullu
  • 3,477
  • 7
  • 43
  • 70
19
votes
1 answer

Can't get multi-mapping to work in Dapper

Playing around with Dapper, I'm quite pleased with the results so far - intriguing! But now, my next scenario would be to read data from two tables - a Student and an Address table. Student table has a primary key of StudentID (INT IDENTITY),…
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
19
votes
2 answers

Using IAsyncEnumerable with Dapper

We have recently migrated our ASP.NET Core API which uses Dapper to .NET Core 3.1. After the migration, we felt there was an opportunity to use the latest IAsyncEnumerable feature from C# 8 for one of our endpoints. Here is the pseudocode before the…
Ankit Vijay
  • 3,752
  • 4
  • 30
  • 53
19
votes
4 answers

An enumerable sequence of parameters (arrays, lists, etc) is not allowed in this context in Dapper

I have the following code: static void Main(string[] args){ string sql= "SELECT * FROM Posts WHERE 1=1 "; SqlParameter[] @params= SetDynamicParameter(ref sql, "Param=Value", "Param2=Value2", "ParamN=ValueN"); IDbConnection…
Ko.Y
  • 325
  • 1
  • 2
  • 7
19
votes
2 answers

How do I pass a table-valued parameter to Dapper in .NET Core?

I am using .NET Core and Dapper. My problem is that .NET Core doesn't have DataTables, and that's what Dapper uses for table-valued parameters (TVP). I was trying to convert a List to a List, create a SqlParameter with this list…
Nauzet
  • 661
  • 8
  • 20
19
votes
6 answers

Dapper and Enums as Strings

I am trying to use Dapper and Dapper-Extensions and to serialize my enums on the database as string. Right now they are serialized as integers (inside a VARCHAR field) instead. Is there any way to do this? Any custom type mapping that I can add? I…
MaurGi
  • 1,698
  • 2
  • 18
  • 28