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
323
votes
11 answers

SELECT * FROM X WHERE id IN (...) with Dapper ORM

What is the best way to write a query with IN clause using Dapper ORM when the list of values for the IN clause is coming from business logic? For example let's say I have a query: SELECT * FROM SomeTable WHERE id IN…
Marko
  • 12,543
  • 10
  • 48
  • 58
248
votes
9 answers

Performing Inserts and Updates with Dapper

I am interested in using Dapper - but from what I can tell it only supports Query and Execute. I do not see that Dapper includes a way of Inserting and Updating objects. Given that our project (most projects?) need to do inserts and updates, what…
Slaggg
  • 6,381
  • 7
  • 28
  • 27
235
votes
6 answers

Is there a way to call a stored procedure with Dapper?

I am very impressed with the results of Dapper Micro ORM for stackoverflow.com. I am considering it for my new project and but I have one concern about that some times my project requires to have Stored Procedure and I have search a lot on web but…
Jalpesh Vadgama
  • 13,653
  • 19
  • 72
  • 94
233
votes
9 answers

How do I perform an insert and return inserted identity with Dapper?

How do I perform an insert to database and return inserted identity with Dapper? I've tried something like this: string sql = "DECLARE @ID int; " + "INSERT INTO [MyTable] ([Stuff]) VALUES (@Stuff); " + "SELECT @ID =…
ppiotrowicz
  • 4,464
  • 3
  • 32
  • 46
225
votes
17 answers

Manually map column names with class properties

I am new to the Dapper micro ORM. So far I am able to use it for simple ORM related stuff but I am not able to map the database column names with the class properties. For example, I have the following database table: Table Name: Person person_id …
user1154985
  • 2,253
  • 3
  • 14
  • 4
160
votes
8 answers

How do I map lists of nested objects with Dapper

I'm currently using Entity Framework for my db access but want to have a look at Dapper. I have classes like this: public class Course{ public string Title{get;set;} public IList Locations {get;set;} ... } public class Location{ …
b3n
  • 3,805
  • 5
  • 31
  • 46
152
votes
6 answers

How to use transactions with dapper.net?

I would like to run multiple insert statements on multiple tables. I am using dapper.net. I don't see any way to handle transactions with dapper.net. Please share your ideas on how to use transactions with dapper.net.
Amit
  • 25,106
  • 25
  • 75
  • 116
140
votes
7 answers

Correct use of multimapping in Dapper

I'm trying to use the multimapping feature of Dapper to return a list of ProductItems and associated Customers. [Table("Product")] public class ProductItem { public decimal ProductID { get; set; } public string ProductName { get;…
Richard Forrest
  • 3,567
  • 2
  • 23
  • 32
130
votes
4 answers

Adjusting CommandTimeout in Dapper.NET?

I'm trying to run SQL backups through a stored procedure through Dapper (the rest of my app uses Dapper so I'd prefer to keep this portion running through it as well). It works just fine until the CommandTimeout kicks in. using (var c =…
sh-beta
  • 3,809
  • 7
  • 27
  • 32
112
votes
9 answers

How do I handle Database Connections with Dapper in .NET?

I've been playing with Dapper, but I'm not sure of the best way to handle the database connection. Most examples show the connection object being created in the example class, or even in each method. But it feels wrong to me to reference a…
Donald Hughes
  • 6,627
  • 8
  • 35
  • 46
112
votes
3 answers

How to insert a C# List to database using Dapper.NET

Using dapper, how can I insert a C# List to database. Previously without dapper I used the below code to insert the List values to database. try { connection.Open(); for (int i = 0; i < processList.Count; i++) { …
Praveen
  • 55,303
  • 33
  • 133
  • 164
103
votes
3 answers

How to create arguments for a Dapper query dynamically

I have a dictionary of values Eg "Name": "Alex" Is there a way to pass this to Dapper as arguments for a query? Here is an example showing what I want to do. IDictionary args = GetArgsFromSomewhere(); string query = "select * from…
Cogslave
  • 2,513
  • 3
  • 25
  • 35
99
votes
4 answers

Dapper.NET and stored proc with multiple result sets

Is there any way to use Dapper.NET with stored procs that return multiple result sets? In my case, the first result set is a single row with a single column; if it's 0 then the call was successful, and the second result set will contain that actual…
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
93
votes
6 answers

How do I write one to many query in Dapper.Net?

I've written this code to project one to many relation but it's not working: using (var connection = new SqlConnection(connectionString)) { connection.Open(); IEnumerable stores = connection.Query, Store> …
TCM
  • 16,780
  • 43
  • 156
  • 254
91
votes
6 answers

Does Dapper support the like operator?

Using Dapper-dot-net... The following yields no results in the data object: var data = conn.Query(@" select top 25 Term as Label, Type, ID from SearchTerms WHERE Term like '%@T%'", new { T = (string)term…
Jay Stevens
  • 5,863
  • 9
  • 44
  • 67
1
2 3
99 100