This is Part 12 of a series on using Dapper to simplify data access with ADO.NET

In a previous post, we discussed inserting multiple entities in a loop.

In this post, we will look at a different way of achieving the same thing.

We will use the following type:

public class FieldAgent
{
    public int AgentID { get; }
    public string Name { get; } = null!;
    public DateTime DateOfBirth { get; }
    public AgentType AgentType { get; }
    public string? CountryOfPosting { get; }
    public bool HasDiplomaticCover { get; }
}

Next, we write a query to insert FieldAgents

INSERT dbo.Agents
(
   Name,
   DateOfBirth,
   CountryOfPosting,
   HasDiplomaticCover,
   AgentType
)
VALUES
(
   @Name, @DateOfBirth, @CountryOfPosting, HasDiplomaticCover, @AgentType
) 

Finally, we create our endpoint:

app.MapPost("/", async (SqlConnection cn) =>
{
    // Create query to insert
    const string sql = """
                       INSERT dbo.Agents
                           (
                               Name,
                               DateOfBirth,
                               CountryOfPosting,
                               HasDiplomaticCover,
                               AgentType
                           )
                       VALUES
                           (
                               @Name, @DateOfBirth, @CountryOfPosting, HasDiplomaticCover, @AgentType
                           ) 
                       """;
    // Configure bogus
    var faker = new Faker<FieldAgent>();
    // Generate a full name
    faker.RuleFor(x => x.Name, f => f.Name.FullName());
    // Date of birth, max 90 years go
    faker.RuleFor(x => x.DateOfBirth, f => f.Date.Past(90));
    // Country of posting
    faker.RuleFor(x => x.CountryOfPosting, f => f.Address.Country());
    // Randomly assign diplomatic cover
    faker.RuleFor(x => x.HasDiplomaticCover, f => f.Random.Bool());
    // Agent type is field
    faker.RuleFor(x => x.AgentType, AgentType.Field);

    // Generate  a list of 100 field agents
    var fieldAgents = faker.Generate(100);
    // Now execute the query
    var inserted = await cn.ExecuteAsync(sql, fieldAgents);

    return inserted;
});

If we run this, we will get the following:

CollectionInsert

Our 100 rows have been inserted.

We can verify this by checking the table.

NewSpies

It is important to note that you may have provided a collection of entities to insert, but what is going to happen is the insert query will be run 100 times to insert each FieldAgent.

TLDR

Rather than writing your own loop, you can provide a collection of entities for entry, and Dapper will execute the relevant queries for each element.

The code is in my GitHub.

Happy hacking!