To install these packages, select the project in the Solution Explorer window, then right-click and select “Manage NuGet Packages.” In the NuGet Package Manager window, search for the Dapper.Plus and Microsoft.Data.Sqlite packages and install them.
Alternatively, you can install the Dapper Plus and Dapper packages using the NuGet Package Manager console by entering the commands below.
PM> Install-Package Z.Dapper.Plus
PM> Install-Package Dapper
Insert data in bulk using BulkInsert
To insert bulk data (i.e., multiple entity records) in your database, you can take advantage of the BulkInsert
method. This method will insert multiple rows of data in the underlying database table all at once. Let us examine how to use the BulkInsert
method to insert bulk data in a database.
Consider the following C# class.
class Author
{
public int AuthorId { get; set; }
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public string Address { get; set; } = string.Empty;
public string Phone { get; set; } = string.Empty;
}
The code given below illustrates how you can create a list of authors, populate data, and return the list.
static List GetAuthors()
{
var authors = new List()
{
new Author() { AuthorId = 1, FirstName = "Joydip", LastName = "Kanjilal", Address = "Hyderabad, India", Phone = "1234567890" },
new Author() { AuthorId = 2, FirstName = "Steve", LastName = "Smith", Address = "Texas, USA", Phone = "0987654321" }
};
return authors;
}
You map your entity using the DapperPlusManager.Entity
method as shown below. Note how the Identity
extension method has been called to configure the identity column, i.e., AuthorId
in this example.