<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Dapper on Ravindra Devrani</title><link>https://ravindradevrani.com/tags/dapper/</link><description>Recent content in Dapper on Ravindra Devrani</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Thu, 25 Dec 2025 11:19:51 +0530</lastBuildDate><atom:link href="https://ravindradevrani.com/tags/dapper/index.xml" rel="self" type="application/rss+xml"/><item><title>Dotnet Core API Crud With Mysql and Dapper</title><link>https://ravindradevrani.com/posts/dotnet-crud-with-mysql-and-dapper/</link><pubDate>Thu, 25 Dec 2025 11:19:51 +0530</pubDate><guid>https://ravindradevrani.com/posts/dotnet-crud-with-mysql-and-dapper/</guid><description>&lt;p>In this tutorial, we are going to make dotnet core web api application and perform all the CRUD (create, read, update and delete). I have tried to keep it simple and avoided any complexities like repository pattern.&lt;/p>
&lt;p>&lt;a href="https://github.com/rd003/DotnetPracticeDemos/tree/master/BlogDemos/DapperMysql">💻Source code&lt;/a>&lt;/p>
&lt;h2 id="tech-and-tools-used">Tech and tools used&lt;/h2>
&lt;ul>
&lt;li>Dotnet 10&lt;/li>
&lt;li>MySql 8+ (in docker container. Click &lt;a href="https://ravindradevrani.com/posts/mysql-in-docker/">here&lt;/a>, if you want to create a MySql container in docker)&lt;/li>
&lt;li>Dapper (ORM)&lt;/li>
&lt;li>.NET CLI and VS Code (alternate Visual Studio 2025)&lt;/li>
&lt;/ul>
&lt;h2 id="create-database">Create database&lt;/h2>
&lt;p>Let&amp;rsquo;s start with creating a database and a table. You need to execute this script.&lt;/p></description></item><item><title>Bulk insert in dapper with table valued parameter</title><link>https://ravindradevrani.com/posts/bulk-insert-in-dapper-with-table-valued-parameter/</link><pubDate>Sun, 04 May 2025 17:00:00 +0530</pubDate><guid>https://ravindradevrani.com/posts/bulk-insert-in-dapper-with-table-valued-parameter/</guid><description>&lt;!-- raw HTML omitted -->
&lt;p>There might be instances when you want to insert bulk data. For an instance, you want to create an order, where you need to add multiple items. Let&amp;rsquo;s see how can we insert bulk data in c# using dapper.&lt;/p>
&lt;p>Note: It is only good for adding bunch of rows. But if you are looking for adding hundreds of rows then better to use other approaches. There are many, if you look out.&lt;/p></description></item><item><title>Dapper: Output Parameter</title><link>https://ravindradevrani.com/posts/dapper-output-param/</link><pubDate>Sun, 23 Mar 2025 00:00:00 +0530</pubDate><guid>https://ravindradevrani.com/posts/dapper-output-param/</guid><description>&lt;h2 id="stored-procedure">Stored procedure&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-sql" data-lang="sql">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">CREATE&lt;/span> &lt;span style="color:#66d9ef">OR&lt;/span> &lt;span style="color:#66d9ef">ALTER&lt;/span> &lt;span style="color:#66d9ef">PROCEDURE&lt;/span> [dbo].[CreateTrackEntry]
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">@&lt;/span>EntryDate DATE,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">@&lt;/span>SleptAt DATETIME2,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">@&lt;/span>WokeUpAt DATETIME2,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">@&lt;/span>NapInMinutes SMALLINT,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">@&lt;/span>TotalWorkInMinutes SMALLINT,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">@&lt;/span>Remarks NVARCHAR(&lt;span style="color:#ae81ff">1000&lt;/span>) &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#66d9ef">NULL&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">@&lt;/span>TrackEntryId INT &lt;span style="color:#66d9ef">OUTPUT&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">AS&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">BEGIN&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">-- code removed for brevity
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">END&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>We have a stored procedure that returns &lt;code>TrackEntryId&lt;/code> as an output parameter. Let&amp;rsquo;s see how can we execute it from the dapper?&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;display:grid;">&lt;code class="language-cs" data-lang="cs">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">using&lt;/span> IDbConnection connection = &lt;span style="color:#66d9ef">new&lt;/span> SqlConnection(_connectionString);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">var&lt;/span> parameters = &lt;span style="color:#66d9ef">new&lt;/span> DynamicParameters(trackEntryToCreate);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Input params&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>parameters.Add(&lt;span style="color:#e6db74">&amp;#34;@EntryDate&amp;#34;&lt;/span>, trackEntryToCreate.EntryDate);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>parameters.Add(&lt;span style="color:#e6db74">&amp;#34;@SleptAt&amp;#34;&lt;/span>, trackEntryToCreate.SleptAt);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>parameters.Add(&lt;span style="color:#e6db74">&amp;#34;@WokeUpAt&amp;#34;&lt;/span>, trackEntryToCreate.WokeUpAt);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>parameters.Add(&lt;span style="color:#e6db74">&amp;#34;@NapInMinutes&amp;#34;&lt;/span>, trackEntryToCreate.NapInMinutes);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>parameters.Add(&lt;span style="color:#e6db74">&amp;#34;@TotalWorkInMinutes&amp;#34;&lt;/span>, trackEntryToCreate.TotalWorkInMinutes);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>parameters.Add(&lt;span style="color:#e6db74">&amp;#34;@Remarks&amp;#34;&lt;/span>, trackEntryToCreate.Remarks);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// output params&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex; background-color:#3c3d38">&lt;span>parameters.Add(&lt;span style="color:#e6db74">&amp;#34;@TrackEntryId&amp;#34;&lt;/span>, dbType: DbType.Int32, direction: ParameterDirection.Output);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">await&lt;/span> connection.ExecuteAsync(&lt;span style="color:#e6db74">&amp;#34;CreateTrackEntry&amp;#34;&lt;/span>, parameters,commandType:CommandType.StoredProcedure);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex; background-color:#3c3d38">&lt;span>&lt;span style="color:#66d9ef">int&lt;/span> trackEntryId = parameters.Get&amp;lt;&lt;span style="color:#66d9ef">int&lt;/span>&amp;gt;(&lt;span style="color:#e6db74">&amp;#34;@TrackEntryId&amp;#34;&lt;/span>);
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description></item><item><title>Transactions in Dapper</title><link>https://ravindradevrani.com/posts/transactions-in-dapper/</link><pubDate>Thu, 20 Mar 2025 09:39:47 +0530</pubDate><guid>https://ravindradevrani.com/posts/transactions-in-dapper/</guid><description>&lt;p>Isn&amp;rsquo;t it already described in Dapper docs? Sure it is. Why do I bother to write this? Am I just wtiting it for the sake of &amp;ldquo;posting&amp;rdquo;? No, I am not. Actually, I was trying to write the code by using Dapper&amp;rsquo;s docs. Unfortunately, I ran into a few bugs. I am using .NET 9, by the way and this is not even a blog post; it&amp;rsquo;s just a code snippet. I thought I should share it, may be someone else is facing the same problem as me.&lt;/p></description></item><item><title>Dotnet Core Api CRUD With Dapper and PostgreSql</title><link>https://ravindradevrani.com/posts/dotnet-core-api-crud-with-dapper-and-postgresql/</link><pubDate>Sun, 23 Feb 2025 18:00:17 +0530</pubDate><guid>https://ravindradevrani.com/posts/dotnet-core-api-crud-with-dapper-and-postgresql/</guid><description>&lt;!-- raw HTML omitted -->
&lt;h4 id="source-code-httpsgithubcomrd003postgressdapperdemohttpsgithubcomrd003postgressdapperdemo">💻Source Code: &lt;a href="https://github.com/rd003/PostgressDapperDemo">https://github.com/rd003/PostgressDapperDemo&lt;/a>&lt;/h4>
&lt;h3 id="tools-and-technology-used">Tools and technology used&lt;/h3>
&lt;ul>
&lt;li>VS Code (editor)&lt;/li>
&lt;li>.Net 8&lt;/li>
&lt;li>Postgres&lt;/li>
&lt;li>Dapper&lt;/li>
&lt;/ul>
&lt;p>Let’s get started with creating the database first.&lt;/p>
&lt;p>create database PersonDb;&lt;/p>
&lt;p>Now, create a table within the database.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-sql" data-lang="sql">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">create&lt;/span> &lt;span style="color:#66d9ef">table&lt;/span> Person
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>(
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Id serial &lt;span style="color:#66d9ef">primary&lt;/span> &lt;span style="color:#66d9ef">key&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Name varchar(&lt;span style="color:#ae81ff">30&lt;/span>) &lt;span style="color:#66d9ef">not&lt;/span> &lt;span style="color:#66d9ef">null&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Email varchar(&lt;span style="color:#ae81ff">30&lt;/span>) &lt;span style="color:#66d9ef">not&lt;/span> &lt;span style="color:#66d9ef">null&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>);
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>To create a new project you need to run these commands in a sequence.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-sh" data-lang="sh">&lt;span style="display:flex;">&lt;span>&amp;gt; dotnet new sln -o PostgressDapperDemo
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&amp;gt; cd PostgressDapperDemo
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&amp;gt; dotnet sln add .&lt;span style="color:#ae81ff">\P&lt;/span>ostgressDapperDemo&lt;span style="color:#ae81ff">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ae81ff">&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&amp;gt; code . &lt;span style="color:#75715e">#this command will open this project in the vs code&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="nuget-packages">Nuget packages&lt;/h3>
&lt;p>Install the following nuget packages.&lt;/p></description></item><item><title>Write dapper effectively with generic methods</title><link>https://ravindradevrani.com/posts/write-dapper-effectively-with-generic-methods/</link><pubDate>Wed, 19 Feb 2025 17:40:41 +0530</pubDate><guid>https://ravindradevrani.com/posts/write-dapper-effectively-with-generic-methods/</guid><description>&lt;!-- raw HTML omitted -->
&lt;p>&lt;strong>Dapper&lt;/strong> is a micro ORM for database connectivity in
dotnet and it&amp;rsquo;s been around for quite a while. It is loved by most of
the developers because of its simplicity and performance.&lt;/p>
&lt;h3 id="why-dapper">&lt;strong>Why dapper?&lt;/strong>&lt;/h3>
&lt;p>With the help of dapper you can quickly access the data without writing
too much code. It performs very well because we directly write queries
and executes them. Developers who loves to work with queries and for the
projects, where performance really matters where you wants to write very
optimized queries, dapper can be the good choice.&lt;/p></description></item></channel></rss>