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
29
votes
5 answers

Ignore property on model property

How can I ignore a property on my model using dapper/dapper extensions/dapper rainbow or any of those dapper libraries?
Elisabeth
  • 20,496
  • 52
  • 200
  • 321
29
votes
4 answers

How can I get Dapper to map .net datetime to datetime2?

Pretty simple, I'm converting our existing system from EF to Dapper. For various corporate reasons we can't really change the database, some of the tables have columns that are of type DateTime2. Dapper converts any .net DateTime to…
Dirk
  • 884
  • 1
  • 7
  • 18
28
votes
3 answers

How to implement Generic Repository Design Pattern with Dapper?

I am using Dapper for a MicroORM to retrieve and Save Data to SQL Server 2014. I have got DTO classes in a DTO Proj that represent the Data retrieved from the DB or saved to the DB. I am using the Repository Pattern so at my Service layer if a…
Ctrl_Alt_Defeat
  • 3,933
  • 12
  • 66
  • 116
28
votes
2 answers

How do I use 'Where In' in Dapper

I've been trying unsuccessfully now for a while to use an IEnumerable with a WHERE IN clause in Dapper. In the documentation, it does say that IEnumerable is supported for use in a WHERE IN but I can't even get that to work. Dapper…
Sam
  • 7,245
  • 3
  • 25
  • 37
27
votes
3 answers

Why does Dapper throw an OracleException when i run a query or command with parameters?

I am evaluation dapper but i already running into some problems. I am trying to do this using (IDbConnection connection = GetConnection()) { connection.Open(); var result = connection.Query( "select * from myTable where ID_PK = @a;",…
mrt181
  • 5,080
  • 8
  • 66
  • 86
27
votes
1 answer

How to use dapper with ASP.Net core Identity?

I have a database and iam trying to use dapper with Core Identity to make queries to database. but i am stuck at this point. I am using a User from the interface of identityUser: public class User : IdentityUser { } The with a making a custom user…
Gonzalo
  • 322
  • 1
  • 3
  • 13
27
votes
13 answers

How to generate model from database using Dapper?

I am coming from PetaPoco camp. PetaPoco has a T4 template which generates model from the database. Is anything similar available for Dapper? I installed Dapper using NuGet and added SqlHelper.cs, but I didn't find anything which generates model…
RKh
  • 13,818
  • 46
  • 152
  • 265
26
votes
3 answers

Where to put sql when using dapper?

I'm using dapper for a mvc3 project at work, and I like it. However, how are you supposed to layer the application when using dapper? Currently I just have all my sql stuffed directly in the controller (slap) but I was thinking of making a class…
Christian Wattengård
  • 5,543
  • 5
  • 30
  • 43
26
votes
2 answers

How to pull back all parent/child data in complex object

I have these two tables with a one (category) to many (product) relationship in the database: Table Product Name Description ProductCategory Table Category Category Description And these classes: public class Product { …
LRP
  • 283
  • 1
  • 3
  • 5
25
votes
3 answers

Is there a way to access the columns in a Dapper FastExpando via string or index?

I am pulling in a Dapper FastExpando object and want to be able to reference the column names dynamically at run time rather than at design/compile time. So I want to be able to do the following: var testdata = conn.Query("select * from Ride Where…
Jay Stevens
  • 5,863
  • 9
  • 44
  • 67
25
votes
4 answers

Best way to do bulk inserts using dapper.net

I am using the following code to insert records to a table in SQL Server 2014 using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["myConnString"])) { conn.Execute("INSERT statement here", insertList); } The…
user20358
  • 14,182
  • 36
  • 114
  • 186
25
votes
1 answer

System.Data.SqlClient.SqlConnection does not contain a definition for Query with dapper and c#

The following code when compiling gives the error message below: 'System.Data.SqlClient.SqlConnection' does not contain a definition for 'Query' and no extension method 'Query' accepting a first argument of type…
James Radford
  • 1,815
  • 4
  • 25
  • 40
24
votes
3 answers

Call custom constructor with Dapper?

I'm trying to use Dapper to interface with the ASP.NET SQL Membership Provider tables. I wrapped the SqlMembershipProvider class and added an additional method to get me the MembershipUsers given a certain criteria relating to some custom tables I…
John B
  • 20,062
  • 35
  • 120
  • 170
24
votes
6 answers

Map string to guid with Dapper

I'm using Dapper to hammer out some load testing tools that need to access a PostgreSQL database. This particular version of PostgreSQL does not support GUIDs natively, so GUID values are stored as 32 character strings. The values are converted to…
Marnix van Valen
  • 13,265
  • 4
  • 47
  • 74
24
votes
2 answers

Dapper throws "Invalid type owner for DynamicMethod."

So I'm trying to use Dapper.net and I'm liking it. What I'm not liking is when I try to batch-insert entities and I get the following error thrown: Invalid type owner for DynamicMethod. at System.Reflection.Emit.DynamicMethod.Init(String name, …
Maffelu
  • 2,018
  • 4
  • 24
  • 35