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
1
vote
2 answers

How to get return value from query with Dapper?

I'm trying to get a return value from an insert query using Dapper. Here's how I try to make it work: // the query with a "returning" statement // note : I have a trigger that sets the Id to a new value using the generator IF Id is null... string…
neggenbe
  • 1,697
  • 2
  • 24
  • 62
1
vote
1 answer

How to Get entity by Unique Key using Dapper.Contrib?

I have an object, assume public class User { [Key] int TheId { get; set; } string Name { get; set; } int Age { get; set; } } TheId is my auto increment column and Name is my unique key. When saving a user, I want to check its…
Omer
  • 8,194
  • 13
  • 74
  • 92
1
vote
1 answer

How to convert a datetime string to datetime in SQL Server

From within my C# app I'm calling a stored procedure with a TVP. A couple of columns are datetime. A call to the SP might look like: declare @p1 dbo.MyTvp insert into @p1 values('2020-03-19 00:00:01','2020-03-30 23:59:59') exec MySp…
Ivan-Mark Debono
  • 15,500
  • 29
  • 132
  • 263
1
vote
1 answer

Dapper Compatibility .Net Core 3.1

Release notes say that Dapper is compatible with .NET Standard 2.0. Why does it work with .Net Core 3.1? Since what release does it work?
ckoch
  • 31
  • 6
1
vote
1 answer

Multiple Dapper Execute() statements inside transaction throws NpgsqlTransaction has completed; it is no longer usable exception

I'm using dapper with npgsql. (.net core 3.1) Multiple connection.Execute(query, params, transaction) for one connection and inside one transaction throws This NpgsqlTransaction has completed; it is no longer usable. exception when calling commit()…
bidev
  • 13
  • 1
  • 4
1
vote
2 answers

Fail to retrieve the User Id from Database using Dapper

public int UserAuthentication(UsersViewModel users) { DynamicParameters parameters = new DynamicParameters(); parameters.Add("@UserName", users.UserName); parameters.Add("@Password", users.Password); …
Krish
  • 167
  • 1
  • 3
  • 16
1
vote
1 answer

Dapper InsertAsync not inserting records

Here is the top of the POCO I am using to create an object to install into my database table called "LOCITEMS": [Table ("LOCITEMS")] public class LOCITEMS :ICloneable { [ExplicitKey] public string LOCATIONID { get; set; } public string…
john
  • 1,273
  • 3
  • 16
  • 41
1
vote
1 answer

Dapper provide default name for dynamic result sets with QueryMultiple

TLDR; Is there a way (using a type map or some other solution) to give dynamic result sets a default name, such as "(No Column Name)" in Dapper when no column name is supplied? I am writing a query editor that allows users to write and run…
Griswald_911
  • 119
  • 8
1
vote
3 answers

Unable to install dapper 2.0.30 with .net framework 4.5

I am learning dapper ORM. While installing it through nuget package manager I am getting following error: Could not install package 'Dapper 2.0.30'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5', …
Amit Kaushal
  • 429
  • 1
  • 9
  • 25
1
vote
1 answer

How to Access Dapper Results When Using Dynamic Due to Enum

I have a Dapper DB call that has a column that maps to an enum on the type. From what I understand, I have to query using dynamic because the query can't map to my Request type since Status is an enum on it. So I tried using this: public async…
Bob Horn
  • 33,387
  • 34
  • 113
  • 219
1
vote
2 answers

Have a base entity class with CRUD implementation and a primary key ID that can be changed by the classes implementing it

In order to not rewrite basic CRUD for each entity I want to implement a base entity class with the base CRUD operations that can be implemented by any other entity. The problem is I'm using Dapper for mapping with Dapper.Contrib and my database…
StefanJM
  • 1,533
  • 13
  • 18
1
vote
1 answer

WPF, Caliburn Micro and Dapper - Datagrid Checkbox binding

I am using Caliburn Micro and Dapper in a WPF Project where I create a DataGrid which I populate with data from a SQL Server Database Table. Please consider the following code snippets: ChangesModel.cs using System; using…
Mysterio
  • 139
  • 12
1
vote
2 answers

Dapper return print message from stored procedure

I have a stored procedure which I can't modify and I'm trying to get the PRINT message that get's returned if an error occurs. The code in my stored procedure is. IF((ISNULL(@ID,''))='') BEGIN PRINT N'Error: This ID is invalid.'; …
usr4896260
  • 1,427
  • 3
  • 27
  • 50
1
vote
1 answer

Dapper ensure that query succeeded best practice

I want to use Dapper into my WebApi Project. I Have a method for creating database from query: public async Task CreateDatabase() { var connection = _connection.GetOpenConnection(); bool created = await…
michasaucer
  • 4,562
  • 9
  • 40
  • 91
1
vote
1 answer

static class configuration for service fabric actors

Service fabric actors are used to access database and all the methods suppose to use Dapper as ORM tool. One thing I found out as the best way to solve current problem is using something called SqlMapper in Dapper. With that, you can define general…
swcraft
  • 2,014
  • 3
  • 22
  • 43