0

I have Event Class that has references to 2 objects Location and Category.

I write my SQL query string, and it works fine but when I put the query in dapper ORM.VS told me I have an inner exception. I am still getting the exception even after I use splitOn param.

Note: we use RecordID as col's name key col for the following tables Event, EventLocation and EventCategory. Is that a problem?

System.ArgumentException occurred HResult=0x80070057 Message=When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id

      public IEnumerable<Event> SelectEventsForList()
        {
            // using (var db = new )

            var queryStr = @"SELECT 
                            e.RecordID
                            ,e.Title
                            ,e.Description [Description]
                            ,e.Location  [LocationDetails]
                            ,e.RegistrationRequired AS [IsRegistrationRequired]
                            ,e.StartDate AS [EventDate]
                            ,e.StartDate
                            ,e.EndDate
                            ,e.MaximumRegistrants
                            ,eloc.RecordID
                            ,eloc.DisplayName
                            ,eloc.DisplayColour
                            ,ecat.RecordID
                            ,eCat.DisplayName
                            ,eCat.DisplayColour
                            FROM dbo.Event e INNER JOIN dbo.EventLocation eloc ON e.LocationId = eloc.RecordID
                            INNER JOIN dbo.EventCategory eCat ON e.CategoryID = ecat.RecordID
                            WHERE eCat.Deleted = 0";

            return this.dbConnection.Query<Event, Location, Category, Event>(
queryStr, (e, l, c) => { 
e.Location = l; e.Category = c; return e; 
},splitOn: "eloc.RecordID,ecat.RecordID");
        }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
NinjaDeveloper
  • 1,620
  • 3
  • 19
  • 51
  • 2
    Did you try with just _eloc.RecordID,ecat.RecordID_ ? The others id are not involved in your mapping – Steve Apr 27 '17 at 16:02
  • Also, better specify all the field' names instead of using * – Steve Apr 27 '17 at 16:03
  • I remove * and make query move verbose, split the query to Event | Location | Category, and I just used eloc.RecordID,ecat.RecordID still getting the same error. :( .I see the updated code – NinjaDeveloper Apr 27 '17 at 16:13
  • I have an example in my code where I don't add the table specifier but just the field name. IE: RecordID not sure if you need it two times. – Steve Apr 27 '17 at 16:20
  • You can find some light in these answers: http://stackoverflow.com/questions/7472088/correct-use-of-multimapping-in-dapper/7478958#7478958 and http://stackoverflow.com/questions/17168926/when-using-the-multi-mapping-apis-ensure-you-set-the-spliton-param-if-you-have-k?rq=1 – Steve Apr 27 '17 at 16:28

1 Answers1

0

If you write the table name.column name in the select clause, the column name in the resultset will not include the table name. e.g.:

select eloc.RecordId 
from dbo.EventLocation eloc

The column name returned will be RecordId.

So you should use splitOn:"RecordId,RecordId".

Dapper looks through the columns in the resultset backwards, so it should find the 2nd and 3rd RecordId columns returned by the query.

kristianp
  • 5,496
  • 37
  • 56