Accessing data with Dapper
In this recipe, we will learn how to get data from a database with the Dapper Micro ORM in ASP.NET Core.
Getting ready
Dapper was created by Sam Saffron and can be found in his GitHub repository at: https://github.com/SamSaffron/dapper-dot-net.
Let's create a new empty web application with Visual Studio 2017.
How to do it...
We will query the Book table in our SQL Express database:
- First, we will create three folders:
Controllers
,Models
, andViews
. - Next, we add the following NuGet packages to the dependencies to the project:
"Microsoft.AspNetCore.Server.IISIntegration", "Microsoft.AspNetCore.Server.Kestrel", "Microsoft.AspNetCore.Mvc", "Microsoft.AspNetCore.Razor.Tools", "Microsoft.Extensions.Configuration.EnvironmentVariables", "Microsoft.Extensions.Configuration.Json", "System.Data.Common", "System.Data.SqlClient", "Dapper"
- Next, let's create an
appsettings.config
file to add our database connection string and add the following code:
{ "ConnectionStrings": { "CookBookConnection...