How to get values ​​for child objects using Dapper ORM?

I get profile information with the following:

var profiles = connection.Query<Models.PROFILE>("SELECT * FROM PROFILES WHERE ID=@ID ", new { ID = profileID }); // IEnumerable var profile = profiles.First<Models.PROFILE>(); 

The profile object contains other collections, such as profileImages. The problem is that the number of elements for each child is zero. I also want to get data, say, profileImages.

Is there something that needs to be configured to query child objects, and if so, is it possible to indicate which one and how many levels?

I also tried multimapping:

  var profiles = connection.Query<Models.PHOTOS_PERMISSIONS, Models.PROFILE, Models.PHOTOS_PERMISSIONS>(sql, (p1, p2) => { p1.ID = profileID; return p1; }, new { ID = profileID }, splitOn: "OWNER_PROFILESIDFK, ID").AsQueryable(); 

PHOTOS_PERMISSIONS.OWNER_PROFILESIDFK = PROFILE.ID

And getting the following error:

When using the multiple display APIs, be sure to specify the splitOn parameter if you have keys other than Id Parameter name: splitOn

I tried options for what is splitOn in my text, but still getting the same error.

+5
source share
1 answer

Dapper does not support One-To-Many mapping like this. Check out this question, this may help.

One-to-many multi-mapping

If your PROFILEIMAGES table has FK on the PROFILES identifier, you can send 2 queries and use the GridReader.

 var sql = @" select * from PROFILES where profileId= @id select * from PROFILEIMAGES where OWNER_PROFILESIDFK = @id"; using (var multi = connection.QueryMultiple(sql, new {id=selectedId})) { var profile = multi.Read<Models.PROFILE>().Single(); profile.ProfileImages = multi.Read<Model.PROFILEIMAGES>().ToList(); } 
+8
source

All Articles