简体   繁体   中英

Still getting the exception even after I use splitOn param

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");
        }

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM