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
24
votes
3 answers

Can I use dapper-dot-net with Entity Framework?

I am trying to use dapper-dot-net to speed up some area of my asp.net mvc application. I am using EF5 Code first also. Since dapper-dot-net is just some extensions for IDbConnection, can i just use DbContext.Database.Connection to use…
liuhongbo
  • 2,051
  • 4
  • 22
  • 29
24
votes
5 answers

Dapper. Map to SQL Column with spaces in column names

I've managed to get something up and running today as small sandbox/POC project, but have seemed to bump my head on one issue... Question: Is there a way to get dapper to map to SQL column names with spaces in them. I have something to this…
Rohan Büchner
  • 5,333
  • 4
  • 62
  • 106
23
votes
4 answers

Dapper with Attributes mapping

I try to map my Id fields with the Column Attributes but for some reason this doesn't seem to work and I can't figure out why. I set up a test project to demonstrate what I am trying. First, I got my 2 entities: Entity Table1 using…
Cornelis
  • 1,729
  • 5
  • 23
  • 39
23
votes
5 answers

How to capture all SQL sent over Ado.Net

I have an application that uses both Entity Framework and Dapper. I would like to provide a custom logger to log out any sql that is issued over the ado.net connection. What is the best way of doing this? Alternately, if it's not easily possible…
George Mauer
  • 117,483
  • 131
  • 382
  • 612
23
votes
3 answers

Why doesn't Dapper dot net open and close the connection itself?

Dapper implicitly expects a connection to be open when it uses it. Why doesn't it open and close it itself? Wouldn't this simply connection management? I ask because a co-worker and I have been going back and forth on the nature of what goes on…
smdrager
  • 7,327
  • 6
  • 39
  • 49
23
votes
4 answers

Can I specify DB column names for dapper-dot-net mappings?

Is there a way with dapper-dot-net to use an attribute to specify column names that should be used and not the property name? public class Code { public int Id { get; set; } public string Type { get; set; } // This is called code in the…
Walter Fresh
  • 365
  • 2
  • 4
  • 10
23
votes
3 answers

Dapper MultiMap doesn't work with splitOn with NULL value

I have a problem with MultiMaps in dapper trying to split on column that contains NULL. Dapper seems not to instantiate object and my mapping function receives null instead of object. Here's my new test: class Product { public int Id…
Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
22
votes
3 answers

Dapper with .NET Core - injected SqlConnection lifetime/scope

I'm using .NET Core Dependency Injection to instantiate a SqlConnection object during the application startup, which I'm then planning to inject in my repository. This SqlConnection will be used by Dapper to read/write data from the database within…
Philip P.
  • 1,162
  • 1
  • 6
  • 15
22
votes
4 answers

Pass Table Name as Parameter to Dapper

Is it possible to pass in the table name as a parameter to a Dapper Query command? I'm not looking for a SQL table defined function or a SQL table variable. I want to define the table name within C# and pass it to Dapper. Here's my code, that…
bigmac
  • 2,553
  • 6
  • 38
  • 61
22
votes
2 answers

Dapper Multi Mapping with QueryMultiple

I have a stored procedure that returns multiple result sets. I'm executing this with dapper. One of the result sets is Person JOIN Checks, where Person can have many Checks. The end goal is to have distinct person objects that have a collection of…
Ronnie Overby
  • 45,287
  • 73
  • 267
  • 346
22
votes
2 answers

sp_executesql is slow with parameters

I'm using dapper-dot-net as an ORM and it produces the following, slow-executing (1700ms), SQL code. exec sp_executesql N'SELECT TOP 5 SensorValue FROM "Values" WHERE DeviceId IN (@id1,@id2) AND SensorId = @sensor AND SensorValue !=…
m__
  • 1,721
  • 1
  • 17
  • 30
21
votes
3 answers

Unit Testing Dapper with Inline Queries

I know there are several question similar to mine. Dapper: Unit Testing SQL Queries Testing Dapper Queries butI don't think both of above question has clear answer that fit my requirement. Right now I develop a new WebAPI project and split…
Martin Valentino
  • 1,002
  • 2
  • 11
  • 26
21
votes
2 answers

Using Dapper.TVP TableValueParameter with other parameters

I have a procedure that takes in a table-valued parameter, along with others: CREATE PROCEDURE [dbo].[Update_Records] @currentYear INT, @country INT, @records Record_Table_Type READONLY AS and am trying to call this with…
CodingWithSpike
  • 42,906
  • 18
  • 101
  • 138
20
votes
4 answers

Using Dapper with Oracle stored procedures which return cursors

How would one go about using Dapper with Oracle stored procedures which return cursors? var p = new DynamicParameters(); p.Add("foo", "bar"); p.Add("baz_cursor", dbType: DbType.? , direction: ParameterDirection.Output); Here, the DbType is…
hark
  • 203
  • 1
  • 3
  • 4
20
votes
1 answer

dapper -multi-mapping: flat sql return to nested objects

I have a company that contains an address object. The SQL return is flat, and I'm tring to get Query<> to load all the objects. cnn.Query("Sproc", (org,mail,phy) => { …
Omnia9
  • 1,563
  • 4
  • 14
  • 39