18

So I have a situation where I have to join (and map) more than 7 entities (which as far as I see is the current limitation of Dapper). This is what I've got so far (pseudo code):

using (var connection = new SqlConnection(_connectionString)) { IEnumerable<BigEntity> results = connection.Query<BigEntity, Lookup1, Lookup2, ... around 10 of them>(sql, (b, l1, l2, l3) => { // map and return here }, splitOn: "split1, split2 ..."); } 

Is there any way around this limitation ? Anyone faced this before ? Some Dapper extensions perhaps ?

4
  • Modifying dapper shouldn't be that hard Commented Dec 19, 2013 at 6:34
  • @talles, nope it's not that hard, you're right. However I'd like to have it official and not have my own patched up version of Dapper. Commented Dec 19, 2013 at 6:47
  • Any updates on this concern? We are hitting the same issue :-( ~Hejo Commented Feb 15, 2017 at 11:12
  • @Hejo If it helps, I ended up adding a new (wrapper) POCO with all the fields I need and mapped to it. Commented Feb 15, 2017 at 11:15

2 Answers 2

17

There is a merged PR on this topic from Sep 2014:

https://github.com/StackExchange/Dapper/pull/158/files

The PR added methods where you can pass an array of Types. So the limitation to 7 entities does not exist anymore on these methods.

This is a Code Test from the Dapper Repo showing how to use one of these new methods:

public async Task TestMultiMapArbitraryWithSplitAsync() { const string sql = @"select 1 as id, 'abc' as name, 2 as id, 'def' as name"; var productQuery = await connection.QueryAsync<Product>(sql, new[] { typeof(Product), typeof(Category) }, (objects) => { var prod = (Product)objects[0]; prod.Category = (Category)objects[1]; return prod; }); var product = productQuery.First(); // assertions product.Id.IsEqualTo(1); product.Name.IsEqualTo("abc"); product.Category.Id.IsEqualTo(2); product.Category.Name.IsEqualTo("def"); } 
Sign up to request clarification or add additional context in comments.

Comments

6
+25

Currently the only two ways that I know of to work around this is to

  • Create a POCO class with the fields you want and use your query like a table
  • Modify Dapper's source code to allow for more mappings.

2 Comments

I appreciate your response +1. However, with a risk of sounding like a jerk, this is not much of an answer. The whole reason I'm asking the question is so that I don't have to modify Dapper or create a wrapper POCO. Cheers.
Completely understandable :) I should probably have prefaced this and said that I did a ton of research into this and others have had similar issues and its a hard limit in the system and people have made pull requests requesting this exact feature. I could not find anything like what you are looking for when I needed it for a requirement. Sorry.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.