Still getting the exception even after I use splitOn param

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");
        }
c#
asp.net
sql-server
orm
dapper
asked on Stack Overflow Apr 27, 2017 by NinjaDeveloper • edited Aug 18, 2018 by marc_s

1 Answer

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.

answered on Stack Overflow Mar 25, 2019 by kristianp

User contributions licensed under CC BY-SA 3.0