dapper querymultiple spliton error

asp.net-mvc dapper

Question

I'm using: ASP.NET MVC, MySql, Dapper.NET micro-orm I made a stored procedure with 3 SELECTs, two of which returns lists and the third one returns an integer.

Here is my code:

using (var conn = new MySqlConnection(GetConnectionString()))
{
    var readDb = conn.QueryMultiple(storedProcedure, parameters, commandType: CommandType.StoredProcedure);

    var result = new someView
    {
        TopicsList = readDb.Read<ITopic>().ToList(),
        TopTopicsList = readDb.Read<IMessage>().ToList(),
        TopicsCount = readDb.Read<int>().Single()
    };

    return result;
}

In ITopic I have TopicId, in IMessage I have MessageId.

And here's the error:

When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id Parameter name: splitOn

I tried adding splitOn on both QueryMultiple and Read, and nigher accepted it.

Though I dont understand why I need splitOn? can't dapper see that I have three separate SELECTs? When using conn.Read(storedProcedure,parameters) on each of the selects separately (instead of MultipleQuery on all of the together) dapper has no problem mapping it to a given object.

What am I doing wrong?

Accepted Answer

1) Problem solved when I used the real models names instead of their interfaces names:

TopicView instead of ITopic, TopTopicsView instead of IMessage;

2) Once that was fixed and there was no longer "no splitOn" error, started another problem with the < int > casting in line:

TopicsCount = readDb.Read<int>().Single()

probably mysql doesnt return numbers back as ints?

I tried using decimal, object, dynamic, etc.. with no luck. Eventually fixed it by creating another Model with int property inside that has the same name as the database int parameter and now it works.

3) Here's the final working code:

using (var conn = new MySqlConnection(GetConnectionString()))
{
    var parameters = context.MapEntity(query);

    var multi = conn.QueryMultiple(storedProcedure, parameters, commandType: System.Data.CommandType.StoredProcedure);

    var TopicsList = multi.Read<TopicView>().ToList();
    var TopTopicsList = multi.Read<TopTopicsView>().ToList();
    var result = multi.Read<HomeView>().Single();

    result.TopicsList = TopicsList;
    result.TopTopicsList = TopTopicsList;

    return result;
}



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why