...

/

Using Dapper Multi-Mapping to Resolve the N+1 Problem

Using Dapper Multi-Mapping to Resolve the N+1 Problem

Learn to use Dapper multi-mapping to resolve the N+1 problem.

We'll cover the following...

Wouldn't it be great if we could get the questions and answers in a single database query and then map this data to the hierarchical structure that we require in our data repository? Well, this is exactly what we can do with a feature called multi-mapping in Dapper. Let's look at how we can use this. Follow these steps:

  1. In the data repository, let's change the implementation of the GetQuestionsWithAnswers method to call a single stored procedure:

Press + to interact
public IEnumerable<QuestionGetManyResponse>
GetQuestionsWithAnswers()
{
using (var connection = new
SqlConnection(_connectionString))
{
connection.Open();
return connection.Query<QuestionGetManyResponse>(
"EXEC dbo.Question_GetMany_WithAnswers");
}
}

This is a good start but the ...