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
1 answer

How to add Table Type as a Out Parameter to Dapper Stored Procedure in C#

I'm using dapper to connect to Oracle Database for my .Net Core Microservices application. In a stored procedure, I have one out parameter like this. PROCEDURE INSERT_PLAN_TEST ( ... other parameters goes here.... P_PARENT_PLAN_ID …
55SK55
  • 621
  • 2
  • 8
  • 23
1
vote
1 answer

Deserialize Dapper Query into Model

I have two models: public class RuleValidation { public int RuleId { get; set; } public string RuleName { get; set; } public int ClientId { get; set; } public string ClientName { get; set; } public List
john
  • 1,273
  • 3
  • 16
  • 41
1
vote
1 answer

Execute Dynamic Entity in Database using Dapper

My user send dynamic entity from client-project so, I have to write methods like this public Task FindByNameAsync(string normalizedUserName, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); …
mgsdew
  • 729
  • 1
  • 8
  • 27
1
vote
1 answer

How to mock method properly to return specific data when checking other method with Autofac.Moq?

Have such C# code and try to check method IsFailureProcessStatus to return true. Query method from dapper class SqlMapper which call stored procedures with parameters. public class DatabaseManager : IDatabaseManager { private readonly…
1
vote
2 answers

Dapper data serialization problem on Dot Net Core 3.0

I have a strange error with Dapper (2.0.30) on new DotNet Core 3.0. I am trying to load data from database with this command: await connection.QueryAsync(command)).AsList() And I want to return result as json return this.Ok(Database.Load("SELECT *…
hazzard03
  • 39
  • 1
  • 8
1
vote
1 answer

Proper solution to saves to multiple tables

I'm creating ASP.NET Core application using Dapper as ORM. What is the proper flow to saves objects into multiple tables? In my app architecture I got standard web api controllers that invoke command/query handlers that calculate/invoke other…
CSharpBeginner
  • 1,625
  • 5
  • 22
  • 36
1
vote
1 answer

C# / Dapper - passing DataTable as an input parameter to a stored procedure

In C#, I use Dapper to interact with SQL Server. I call a stored procedure that accepts 2 user-defined tables as parameters. And I am passing a DataTable to my stored procedure. According to this page, the syntax to pass a DataTable is like…
thecodeexplorer
  • 363
  • 1
  • 6
  • 18
1
vote
1 answer

Dapper Multiple Mapping split on First field in select

I have a base class which holds two sub classes, and the base class has no extra properties. I want to use dapper multiple mapping to map the data to sub classes directly. But in split-on property it gives and exception saying "When using the…
Ambareesh
  • 81
  • 1
  • 8
1
vote
1 answer

Dapper.Query> Return a collection of Nulls

Simple Issue (EDITED: To show easy reproducible example first, then the detailed scenario) Having the following Classes: public class SalesMetrics { public decimal SalesDollars { get; set; } public decimal SalesUnits { get; set; } } public…
1
vote
1 answer

Dapper Extensions custom ClassMapper isn't called on Insert()

I'm using Dapper Extensions and have defined my own custom mapper to deal with entities with composite keys. public class MyClassMapper : ClassMapper where T : class { public MyClassMapper() { // Manage unmappable…
1
vote
3 answers

Get specific column from a dapper result set

I'm new using Dapper and I want a better way to fetch data from a certain column in a Dapper row. Data is being retrieved from a stored procedure that returns 1 or more rows with 5 columns but I only need to get 1 column data for this specific…
1
vote
2 answers

Issue retrieving Image in bytes from Database to IFormFile from model class

After creating a POST method for my API that allows me to upload images to the database where I hold them as bytes, I wanted to create a GET method that will allow me to get their information and eventually show them on a web page. My model class…
Questieme
  • 913
  • 2
  • 15
  • 34
1
vote
1 answer

Dapper SplitOn Repeating Classes

I fear I may be misunderstanding multi-mapping in Dapper. Given the following: public class Job { public int JobId { get; set; } public Site FromSite { get; set; } public Site ToSite { get; set; } } public class Site { public int…
SWa
  • 4,343
  • 23
  • 40
1
vote
1 answer

Parameter names for Dapper multi-item Execute

With this code using Dapper .Execute: using var c = new SqlConnection(ccstr); var lst = new[] { 1, 2, 3 }; c.Execute("select @p", lst); // @p not recognized as parameter name Is there a way to have a parameter name (here @p) for this native object…
Pasi Savolainen
  • 2,460
  • 1
  • 22
  • 35
1
vote
1 answer

Get the count of resultsets returned from dapper.QueryMultiple Method

I use Dapper library. I have a dynamic query which returns one or more resultsets/tables from QueryMultiple Method. I don't have any specific count of resultsets to write no. of Read() method. Do we have any function or method (e.g. result.Count =…
SanketS
  • 963
  • 1
  • 13
  • 36