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

Get inserted ID back from SqlConnection.Execute stored procedure

I'm inserting data into my database like so: using (IDbConnection conn = new SqlConnection(connectionString)) { conn.Execute(storedProcedure, parameters, commandType: CommandType.StoredProcedure); } I've added select SCOPE_IDENTITY(); to my…
HeyMan12388
  • 89
  • 1
  • 7
1
vote
1 answer

Map columns to arrays in Dapper

I have a database table with a lot of columns forming something like array: Create table MyTable ( Id int, SomeColumn varchar, OtherColumn varchar, x1 double, x2 double, x3 double, x4 double, ... x50 double ) Now, I have c# class…
1
vote
1 answer

How to return dapper dynamic query when asp.net core api method type is IEnumerable and without newtonsoft.json reference

I hope return dapper dynamic query when asp.net core api method type is IEnumerable and without newtonsoft.json reference. If asp.net core api method type is IEnumerable and using Dapper dynamic query system'll throw exception…
Wei Lin
  • 3,591
  • 2
  • 20
  • 52
1
vote
1 answer

How to Return Data from connection.QueryFirst in ASP.NET Core API

Hi Guys I am learning ASP.NET core how to return data from SQLquery to json current i am getting everything null in result MY Controller Code snippet [HttpGet] [Route("GetData")] public DataMaster GetData() { using (SqlConnection connection =…
Parth Vyas
  • 11
  • 1
1
vote
1 answer

dapper how to map to private set property methods

I know this general question is addressed in lots of areas but not for this specific scenario. I have the following objects public class AuditRecord { public long Id {get; private set; } public Collaborator IssuedBy { get; private set;…
Steven
  • 860
  • 6
  • 24
1
vote
1 answer

Is there a way to call a stored MySQL procedure include insert parameters with Dapper in C#?

public void InsertUser(string firstname, string lastname, string email, string password, int age) { using (IDbConnection connection = new MySql.Data.MySqlClient.MySqlConnection(Helper.CnnVal("Database"))) { List users = new…
Mily
  • 11
  • 1
1
vote
2 answers

Insert JObject list to SQL Server with dapper

I need to insert the result of my JObject to an SQL Database. However it keeps saying I'm not allowed to insert arrays, lists? What am I doing wrong? This is what I got so far: JArray jsonArray = JArray.Parse(sb.ToString()); var jsonObjects =…
SqlKindaGuy
  • 3,501
  • 2
  • 12
  • 29
1
vote
1 answer

double value Dropping scale value after saving to MS SQL using Dapper DynamicParameters

I am missing a scale (see example image below). Even though I specified the precision and scale in my dynamic parameter, I am still loosing the last non zero value. Not sure what I missed out to do. const string insertSql = @"insert into DATATABLE…
dantz
  • 37
  • 1
  • 9
1
vote
1 answer

How to read file in chunks from database with Dapper in dot net standard app

I have created a dot net standard app for central place for storing files. I've managed to upload file in chunks cuz basically I leave the client to send the chunks and just append them as a stream in the database but the problem comes when I want…
S.Minchev
  • 73
  • 8
1
vote
1 answer

Is Dapper query executed in QuerySingle one transaction?

Does dapper execute all query statements which are part of one query in QuerySingle method as one transaction? Example IF NOT EXISTS (SELECT 1 FROM table WHERE ... INSERT INTO TABLE...
Denys Alexieiev
  • 224
  • 1
  • 15
1
vote
1 answer

WPF, Caliburn.Micro and Dapper ComboBoxes

I am just new to WPF, Caliburn.Micro and Dapper. I have three combo boxes: the first one is for the region, the second one is for the provinces in the particular selected region and the third one are the cities in the particular selected province.…
Pragmatix
  • 13
  • 3
1
vote
1 answer

How to change initial catalog at runtime when using a connection string and dapper

I'm writing an MVC C# application. I use dapper as a lightweight ORM. My connection strings are defined with server and initial catalog, and currently if I need to access a different database I define another connection string, and use Ninject…
erikrunia
  • 2,371
  • 1
  • 15
  • 22
1
vote
3 answers

Update Table SQL Query: SqlConnection

I would like to update a record based on Id parameter, I have tried following step but that does not seem correct way to do because it generates compilation error: public async Task CustomerUpdateAsync(string customerId) { await using var…
Urgen
  • 1,095
  • 3
  • 19
  • 39
1
vote
1 answer

Should I actually use async-await in inner methods?

Let's say I have 3 methods as follows: public async Task CalculateData(int index, int trustLevel, string type) { var x = await CalculateBaseScore(index, type); return ComplexProcess(x, trustLevel); } public async Task
Léster
  • 1,177
  • 1
  • 17
  • 39
1
vote
1 answer

Send and receive Datatable using Dapper

I have a model inside another model like: First Model: public class ReminderPushNotificationTest { public string UserName { get; set; } .... public ReminderPushNotificationTableType AndroidResultTableType { get; set; } } Second…
Ruben
  • 155
  • 1
  • 9