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

Does Npgsql support async?

From this question it seems that Npgsql doesn't support async so I start to replace all my calls by synchronous ones, but the answer is very old and I would like to know if it still applies AsyncQuery with postgresql and dapper using npqsql This is…
geckos
  • 5,687
  • 1
  • 41
  • 53
1
vote
1 answer

Read/Write distribution over MySQL Master/Slaves using Dapper

Would dapper have any problems with using two IDbConnection objects for distributing Writes and Reads between MySQL Master/Slaves?
Kynth
  • 2,597
  • 1
  • 18
  • 24
1
vote
1 answer

Dapper map subquery value to property

I'm using Dapper to fetch and map data. I've the following query and relevant class. SELECT pinv.* , sp.SupplierId,sp.SupplierName,sp.Contact1,sp.SupplierAddress,ac.AccountId,ac.AccountName, (Select Count(*) from ReturnInvoices rinv Where…
Hammas
  • 1,126
  • 1
  • 12
  • 37
1
vote
1 answer

Map to nested object using Dapper

I have an object that has 2 lists of classes. public class Order { int order_id; double amount; List order_items; List order_shipments; } And here's how the objects should be mapped: SELECT * FROM Orders…
Joe Defill
  • 439
  • 1
  • 4
  • 16
1
vote
1 answer

Problem with Code-First Database and PostgreSql

I am using Code-First project and my database is PostgreSql. When I migrated my project to PostgreSql by using Npgsql v.2.2.7 everything worked fine. I need to use the new version of Npsql. To achieve my goal I upgraded Npsql to the latest version…
1
vote
2 answers

How to get values from multiple columns as IEnumerable using SqlKata (Dapper)

I'm using SQL and I have a table shown below in my DB. Id Remark1 Remark2 Remark3 Remark4 ------------------------------------------------ 1 AAA BBB CCC DDD 2 EEE FFF GGG HHH …
user4134476
  • 385
  • 4
  • 22
1
vote
1 answer

Postgresql using Dapper to force "ANY(VALUES(), ()...)" for a list

I have a query simply like this: SELECT restaurant_id as Id FROM restaurants WHERE unique_key = ANY(ARRAY['key1', 'key2', ...]) According to the article 100x faster Postgres performance by changing 1 line, using VALUES instead of ARRAY can improve…
pilavust
  • 538
  • 1
  • 7
  • 20
1
vote
1 answer

dapper and running stored procedure ctx_ddl.sync_index

I need to execute the stored procedure ctx_ddl.sync_index on an Oracle database. In a query program this works; EXECUTE ctx_ddl.sync_index('MyIndex'); When I try to run in dapper with the following; using var connect = new…
Xaphann
  • 3,195
  • 10
  • 42
  • 70
1
vote
1 answer

Dapper Resolve Invalid cast exception

I am using Dapper with MySql to get a list of uTeacher with multi-mapping but I can't get it to work! I get Invalid Cast exception from Int32 to uDepartement class and other classes!! ( DataException: Error parsing column 33 (Department=1 - Int32)…
iProg
  • 13
  • 3
1
vote
1 answer

SQL statement to Create Role fails on Postgres 12 using Dapper

I am running Postgres 12 on Windows and have a .Net Core app which uses Dapper as an ORM: The following query works fine: var sql = "SELECT 1 FROM pg_roles WHERE rolname=@un" var result = con.ExecuteScalar(sql, new {un = "someuser"}); Now I'm…
Mats
  • 14,902
  • 33
  • 78
  • 110
1
vote
1 answer

c# Dapper - using linq on QueryAsync method

Why can't I use .select on a queryAsync when it works fine on Query? For example, here the .select yells at me telling me it cant resolve the symbol "select": var result = await sqlConnection.QueryAsync(procedure, …
louuuuuu
  • 13
  • 4
1
vote
1 answer

Dapper - Stored Procedure - Incorrect syntax near 'GO'

I have a stored procedure in a myscript.sql file that looks like this: CREATE PROCEDURE [dbo].[_GetUserID] @EmailAddress NVARCHAR(254) AS DECLARE @UserID UNIQUEIDENTIFIER; SELECT @UserID = [ID] FROM [dbo].[User] WHERE…
Dev
  • 921
  • 4
  • 14
  • 31
1
vote
1 answer

Dapper - multi-mapping APIs ensure you set the splitOn

For the code shown below, I am getting the following error message. *** Error *** When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id (Parameter 'splitOn') From what i understand i am passing the…
fox909a
  • 81
  • 2
  • 11
1
vote
2 answers

Filter Combobox binding source data from another

I have two comboboxes, both with binding sources attached which are pre-populated with data from a SQL server; private void SetLocationAreaBindingSource() { cboLocationArea.DisplayMember = "name"; cboLocationArea.ValueMember…
Shadyjunior
  • 437
  • 3
  • 13
1
vote
2 answers

Dapper only returns Null values

I am facing a problem using Dapper. I have two models: public class ClientEventsModel { public int Id { get; set; } public int ClientId { get; set; } public ClientEventTypeLog EventType {get; set;} public int Value { get; set; } …
Lendim
  • 13
  • 3