0

I want to use dapper to query a complex entity from database.

Ex. Job is top level entity I want to query.

public class Job
{
    public User User { get; set; }
}
public class Account
{
}

public class Roles
{
    public IList<Schedule> Schedules { get; set; }
}


public class Devices
{
    public IList<Roles> Roles { get; set; }
    public IList<Account> Accounts { get; set; }
}

public class Schedule
{

}
public class User
{
    public IList<Account> Accounts { get; set; }
    public Profile Profile { get; set; }
}
public class Profile
{
    public IList<Account> Accounts { get; set; }
}

Is this even possible with dapper, if it is then is it worth using dapper for this or should I use some other ORM (EF, LLBLGen, NH)?

kttii
  • 107
  • 13
Mayank
  • 8,777
  • 4
  • 35
  • 60

1 Answers1

0

I believe you are asking how to implement MultiMapping in Dapper.

You can pass more than one model like so:

var sql = @"select * from Product p 
            inner join Customer c on p.CustomerId = c.CustomerId 
            order by p.ProductName";

var item = connection.Query<ProductItem, Customer, ProductItem>(sql,
        (p, c) => { p.Customer = c; return p; }, splitOn: "CustomerId").First();

Reference: Correct use of Multimapping in Dapper

Community
  • 1
  • 1
kttii
  • 107
  • 13