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

Dapper.NET and IQueryable

Is there a plan to make Dapper.net compatible with IQueryable interfaces? If not, what's the workaround to use Dapper with "Expression Trees" filters?
Bill
  • 2,026
  • 9
  • 55
  • 99
35
votes
3 answers

Using Async Await keywords with Dapper

I want to use a micro-orm and decided to go with Dapper. But can't seem to find any mentions of it supporting the new async/await syntax. Async queries are important for me. Can someone provide a code example of an async query being made with…
Yaron Levi
  • 12,535
  • 16
  • 69
  • 118
34
votes
1 answer

Inserting an IEnumerable collection with Dapper errors out with "class is not supported by Dapper."

Yep, there are questions here and here about how to insert records with dapper-dot-net. However, the answers, while informative, didn't seem to point me in the right direction. Here is the situation: moving data from SqlServer to MySql. Reading the…
IAbstract
  • 19,551
  • 15
  • 98
  • 146
34
votes
13 answers

Fastest way to map result of SqlDataReader to object

I'm comparing materialize time between Dapper and ADO.NET and Dapper. Ultimately, Dapper tend to faster than ADO.NET, though the first time a given fetch query was executed is slower than ADO.NET. a few result show that Dapper a little bit faster…
witoong623
  • 1,179
  • 1
  • 15
  • 32
34
votes
2 answers

How to get Dapper to ignore/remove underscores in field names when mapping?

There are many ways to map database field names to class names, but what is the simplest way to just remove the underscores? public IEnumerable GetPerson(int personId) { using (var dbConnection =…
scw
  • 5,450
  • 8
  • 36
  • 49
34
votes
4 answers

Proper way of using BeginTransaction with Dapper.IDbConnection

Which is the proper way of using BeginTransaction() with IDbConnection in Dapper ? I have created a method in which i have to use BeginTransaction(). Here is the code. using (IDbConnection cn = DBConnection) { var oTransaction =…
Krishnraj Rana
  • 6,516
  • 2
  • 29
  • 36
34
votes
2 answers

What causes "extension methods cannot be dynamically dispatched" here?

Compile Error 'System.Data.SqlClient.SqlConnection' has no applicable method named 'Query' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling…
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
34
votes
1 answer

Dapper AddDynamicParams for IN statement with "dynamic" parameter name

I have simple SQL string like this: "SELECT * FROM Office WHERE OfficeId IN @Ids" The thing is that the @Ids name is entered in an editor so it could be whatever, and my problem is that if I want to pass in, say an array of integers, it only works…
Jens Pettersson
  • 1,167
  • 1
  • 9
  • 14
32
votes
3 answers

Dapper dynamic parameters throw a SQLException "must define scalar variable" when not using anonymous objects

(This code is using Dapper Dot Net in C#) This code works: var command = "UPDATE account SET priority_id = @Priority WHERE name = @Name"; connection_.Execute(command, new { Name = "myname", Priority = 10 } ); This code throws a SqlException: class…
sh-beta
  • 3,809
  • 7
  • 27
  • 32
32
votes
3 answers

is there an ExecuteScalar in Dapper

it looks like there was an ExecuteScalar in Dapper... http://code.google.com/p/dapper-dot-net/issues/attachmentText?id=22&aid=220000000&name=ExecuteScalar.cs&token=9e2fd8899022f507b140ffb883c60e34 Was ExecuteScalar renamed or removed? Can this now…
sgtz
  • 8,849
  • 9
  • 51
  • 91
32
votes
1 answer

Closing connection when using Dapper

Is it necessary to close connection once query is executed explicitly calling Close method or putting the connection within Using statement? Would leaving connection open lead to connection reuse and improve SQL performance for future queries?
Megrez7
  • 1,423
  • 1
  • 15
  • 35
31
votes
1 answer

Using Dapper to populate Enum properties

In using Dapper's Query() function, I am trying to fill in a class that has a property which is an enumerated value. In my database, this column is stored as a byte. However, in the class, they are an enum. In the old ADO.NET approach, I'd…
Slaggg
  • 6,381
  • 7
  • 28
  • 27
31
votes
2 answers

Dapper: Result from "SELECT COUNT(*) FROM TableName

I have the following code: string sql = "SELECT COUNT(*) FROM " + tableName; var rtn = DapperConnection.Query(sql); This works and bring back 1 record in the rtn variable. When I inspect the variable it seems to have 2 members, one is "[0]"…
user2975847
  • 379
  • 1
  • 4
  • 9
31
votes
7 answers

Call stored procedure from dapper which accept list of user defined table type

I have a stored procedure InsertCars which accepts list of user defined table type CarType. CREATE TYPE dbo.CarType AS TABLE ( CARID int null, CARNAME varchar(800) not null, ); CREATE PROCEDURE dbo.InsertCars @Cars AS CarType…
imodin
  • 821
  • 3
  • 12
  • 23
29
votes
1 answer

dapper nuget 1.7 enums mapping

I've encountered an issue after I upgraded to the latest version of Dapper from Nuget (v 1.7). It always return the first enums member (that is, it fail to maps). I am using MySQL as the database. CREATE TABLE `users_roles` ( `userId` INT(11)…
Anton Hasan
  • 551
  • 4
  • 14