Installing and configuring Dapper

In this section, we are going to install and configure Dapper. We will also install a System.Data.SqlClient package that Dapper uses. Let's carry out the following steps:

  1. Let's open the backend project in Visual Studio and go to the Tools menu and then the NuGet Package Manager and choose Manage NuGet Packages for Solution....
NuGet is a tool that downloads third-party and Microsoft libraries and manages the references to them so that the libraries can easily be updated.
  1. On the Browse tab, enter Dapper into the search box.
  2. Select the Dapper package by Sam Saffron, Marc Gravell, and Nick CraverTick our project and click the Install button with the latest stable version selected. Refer to the following screenshot:

We may be asked to accept a licensing agreement before Dapper is downloaded and installed into our project.

  1. Still in the NuGet package manager, search for the System.Data.SqlClient package and install the latest stable version. Refer to the following screenshot:

Note that Microsoft is developing a new SQL client library called Microsoft.Data.SqlClient, which will eventually replace System.Data.SqlClient. When this is released, and when Dapper supports this new library, we'll need to install the Microsoft.Data.SqlClient package instead of System.Data.SqlClient in the preceding step.
  1. Next, we are going to define a connection string in our ASP.NET Core project to our database. In the Solution Explorer, open up a file called appsettings.json to add a ConnectionStrings field that contains our connection string:
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost\SQLEXPRESS;Database=QandA;Trusted_Connection=True;"
},
...
}
The appsettings.json file is a JSON-formatted file that contains various configuration settings for an ASP.NET Core app.

Obviously, change the connection string so that it references your SQL Server and database.

So, that's Dapper installed along with a connection string to our database in place. Next up is to read data from the database using Dapper.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.166.76