Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get values for child objects using Dapper ORM?

Tags:

orm

c#-4.0

dapper

I am retrieving profile details 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 like profileImages. The problem is that the item count for every child object is zero. Also I only want to get data for say, profileImages.

Is there something that needs to be set to query the child objects, and if so, is it possible to specify which one and for how many levels?

I have 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 multi-mapping APIs ensure you set the splitOn param if you have keys other than Id Parameter name: splitOn

I have tried variations of what's in my splitOn text, but still get the same error.

like image 738
ElHaix Avatar asked Apr 19 '12 19:04

ElHaix


People also ask

What is splitOn in Dapper?

splitOn: CustomerId will result in a null customer name. If you specify CustomerId,CustomerName as split points, dapper assumes you are trying to split up the result set into 3 objects. First starts at the beginning, second starts at CustomerId , third at CustomerName .

Does Dapper use SqlClient?

SqlClient and SqlConnection , but Dapper supports other databases that use the DbConnection abstraction. We also need a projection class representing the results of our SQL query.

How do I write a Dapper query?

C# Dapper Queryvar cars = con. Query<Car>("SELECT * FROM cars"). ToList(); The Query method executes the SELECT * FROM cars statement and returns a list of objects.

Is Dapper A ORM?

Dapper is an object–relational mapping (ORM) product for the Microsoft . NET platform: it provides a framework for mapping an object-oriented domain model to a traditional relational database. Its purpose is to relieve the developer from a significant portion of relational data persistence-related programming tasks.


1 Answers

Dapper doesn't support a One-To-Many mapping like this out of the box. Check out this question, it may help though.

Multi-Mapping, one-to-many

If your PROFILEIMAGES table has a FK on PROFILES ID - you could issue 2 querys 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();
} 
like image 173
Alex Avatar answered Oct 03 '22 07:10

Alex