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

Comma seperated in list of strings c# and dapper

I have a class which has this property among others: public class ADUser { public IList Relations { get; set; } } In my database table, Relations is a comma-separated string column. I am using Dapper to get data. This is my…
mohsinali1317
  • 4,255
  • 9
  • 46
  • 85
1
vote
1 answer

Dapper.Net SQL bulk insert decimal precision

I am getting an exception from Dapper Bulk Copy Looks like the underlying bulk copy operation is failing. I dumped the data in json and found the value creating problem is 259815703.3430760631 StackTrace: at…
Anand
  • 1,387
  • 2
  • 26
  • 48
1
vote
1 answer

When I run an Npgsql query using Dapper, why do I get "The field 'XYZ' has a type currently unknown to Npgsql"

I've created the following SQL view in my PostgreSql database (have simplified for brevity): create view "GlossaryView" as select ( select cast( array_agg( cast(quals as "Qualification") ) as…
Ash
  • 2,021
  • 2
  • 26
  • 59
1
vote
1 answer

Mapping a custom object to multiple columns by type handler in Dapper

I'm facing a problem which has been discussed here multiple times, but unfortunately none of the answers or hints work for me. I want to use different custom types (for example money or recordlink). In the database I would store the different…
1
vote
1 answer

Web Api IEnumerable order

Short version: Sorted lists lose their correct ordering when retrieving data via GET. Long version: The function in question consists of 3 steps: Load data with dapper Map data to a different list Return Ok(models) 1) Dapper loading Here we load…
Reasurria
  • 1,808
  • 16
  • 14
1
vote
2 answers

Is there a way to get the column names from a SQL statement?

I'm using Dapper and I want to get all the column names from a SQL statement that is provided at runtime. It should not matter if the resulting relation, returned from the database, is empty or not. public async Task>…
RHofmann
  • 11
  • 1
1
vote
2 answers

Dapper Query Multiple Blank Result Set

I need to be able to tell when a query multiple async in dapper returns back a blank data set (so no rows are returned), but I am not sure of the correct way of doing this. The following is similar to my current code: var data= await…
dmoore1181
  • 1,793
  • 1
  • 25
  • 57
1
vote
1 answer

ConnectionString not initialized .net core

I reproduced a way to access the database with .net Framework but it doesn't work in .net Core. I found a way to fix it and followed what was said but I get the error that the connectionString is not initialized. Therefore, I don't know how to get…
Michaeldc
  • 45
  • 10
1
vote
1 answer

Dapper QueryFirst Too slow if no connection

it takes almost up to 30 seconds, mostly 20ish avg public OperadorViewModel GetById(int id) { IDbConnection con = new SqlConnection(_configuration.GetConnectionString("DefaultConnection")); var query = @"SELECT …
Jackal
  • 3,359
  • 4
  • 33
  • 78
1
vote
1 answer

How to use WILDCARD using Dapper

How can I use LIKE wildcard in postgre DB using Dapper? I have the following code: string query = "SELECT name, index FROM article WHERE prefiks LIKE :prefix ;"; return conn.Query(query , new { prefix = searchingValue }).ToArray(); Where…
Lucas
  • 57
  • 8
1
vote
1 answer

Dapper and execution of multiple same commands?

How does dapper execute multiple times the same command? As I have written below insert sql, and invoke Dapper's execute method with the list parameters, so I will insert 3 objects. The question: does dapper make 3 round trips to Sql Server to…
CSharpBeginner
  • 1,625
  • 5
  • 22
  • 36
1
vote
2 answers

Better solution for updates with Dapper in ASP.NET Core

Let's say we've got a type Book with ten properties. This type is a representation of the table in database. What's the best solution to update such a type? I've used repository pattern where I got update method that take Book type and updates all…
CSharpBeginner
  • 1,625
  • 5
  • 22
  • 36
1
vote
1 answer

System.ObjectDisposedException - Using SQL Connection within Using Block

System.ObjectDisposedException HResult=0x80131622 Message=Cannot access a disposed object. Object name: 'SQLiteConnection'. Source=System.Data.SQLite I've written a simple repository that uses Dapper and DapperExtensions When trying to…
GisMofx
  • 982
  • 9
  • 27
1
vote
1 answer

Can you call a query from inside another query?

Currently I have a stored procedure that does some insert statements, calls another stored procedure which inserts something else and returns an id and then does some update statements. So a bit like this: BEGIN SET NO COUNT ON; BEGIN TRY …
sr28
  • 4,728
  • 5
  • 36
  • 67
1
vote
1 answer

Models.Student' does not contain a definition for 'Score'

I have TWO tables. My primary table is called Student, secondary table is Marks. Dapper is new for me. This is my code: s_id is the primary key in the Student table and is foreign key in Marks table (student (parent) – marks(child)). Controller:…
shashi
  • 37
  • 7
1 2 3
99
100