Hey Reader, In today’s tutorial, I’ll walk you through how to use Dapper, a high-performance micro ORM (object-relational mapper) with a .NET 8 Web API and SQL Server. We’ll cover everything you need to know to build a simple CRUD (Create, Read, Update, Delete) application. Watch on YouTube 📺Check out the full tutorial on YouTube to see everything in action 👇 What We’ll Be DoingWe’re going to work with a simple video game database to start. You’ll learn how to:
First, we’ll set up our Web API using Visual Studio, configure the project, and add Dapper as a NuGet package. Once everything's set, we’ll create a model for our video games that mirrors the database structure: ID, title, publisher, developer, and release date. Next up, you’ll see how to establish a connection to the SQL Server using a connection string in the Why Dapper?Dapper gives you full control over your SQL queries. While it doesn’t auto-generate queries like Entity Framework, Dapper ensures everything stays efficient and lightweight, making it ideal for high-performance apps. Plus, if you're working in an environment where you're dealing with an existing database or a team that handles the database design, Dapper might be perfect for you! Step-by-Step Guide1. Set up the Project:First, let's create a new ASP.NET Core Web API project in Visual Studio. Make sure to select .NET 8. 2. Install Dapper:In the NuGet Package Manager, search for and install 3. Create a Video Game Model:Add a new folder called 4. Add a Connection String:Next, open the
5. Create a Repository Interface:Add a new folder called 6. Create the Repository Implementation:Now, create a class that implements the First, we inject IConfiguration, so that we get access to the connection string from the appsettings.json file. It is necessary to create a connection to the database each time we want to run a query. We also add a little function to create the connection whenever we need it. Next, we implement all the CRUD operations. Let's start with the easy one, receiving all video games. Let's also create the VideoGamesController and add the corresponding GET method there. Last step is registering the repository in the Program.cs. Now we can run the application and test the first call with Swagger. Awesome! Now, let's continue with the other CRUD operations - receiving a single video game, adding a video game, updating and deleting one. Please note, that we're using parameters here. Otherwise, we would risk SQL injection attacks. Don't forget the corresponding controller methods. Wrapping UpNow you have a fully functional API that can interact with a SQL Server database using Dapper. You understand how to fetch data from the database, add new records, update existing ones, and delete records you no longer need. What's your experience with Dapper? Is it your goto tool in your projects or would you rather use Entity Framework or something else? Happy coding and have an awesome weekend! Take care, Patrick PS: Need help understanding .NET & Blazor? There are two ways I can help you with:
PPS: Would you like to sponsor this newsletter? Click here. 💌 |
Become a .NET & Blazor expert with weekly tutorials featuring best practices and the latest improvements, right in your inbox.
Hey Reader, Last week, I tested GPT-5 Agent Mode on .NET and Blazor apps, and the results were surprising. This week, I went a step further. I used GitHub Copilot inside Visual Studio and asked it to create real .NET and Blazor projects for me. Here’s what happened: Copilot generated a working calculator app in C#. It built a Web API that responded with “Hello GPT-5 Agents.” It even created a Blazor app with extra features like a reset button, a plus five counter, and a weather API. Were...
Hey Reader, I just published a new video where I put GPT-5’s Agent Mode to the test. 🚀 Instead of asking it for something simple, I went all in: A .NET 9 console app calculator A Web API with endpoints and docs A Blazor Server app with extra counter buttons And even a Blazor app that calls a public weather API 🎥 Watch it now: In the video, I didn’t just type “make me an app” and hope for the best. I gave GPT-5 very specific prompts, like: “Please create a .NET 9 console application that works...
Hey Reader, I’m back! 🎉 I’ve been on parental leave for the past 2 months, enjoying time with the family. Now I’m diving back into videos and here’s the first one! 🎥 Watch it now: Here’s the TL;DR if you can’t click right now: AI can already write code, fix bugs, and explain complex concepts. So… are we doomed? No. AI isn’t replacing developers anytime soon. But: Developers who use AI will replace those who don’t. The best devs are using GPT to: Skip repetitive work Prototype faster Learn...