1

I have a base class which holds two sub classes, and the base class has no extra properties. I want to use dapper multiple mapping to map the data to sub classes directly. But in split-on property it gives and exception saying

"When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id```

and i am unable to split in the first column itself.

public class User
{
public int UserId {get; set;}
public string UserName {get; set;}
}

public class Business
{
public int BusinessId {get; set;}
public string BusinessName {get; set;}
}

public class UserAndBusiness
{
public User UserDetails{get; set;}
public Business BusinessDetails{get; set;}
}


var userDetails = await dbConnection.QueryAsync<UserAndBusiness, 
User,Business,UserAndBusiness>(@"
                        select User_Id UserId,User_Name UserName,Business_Id BusinessId,
Business_Name BusinessName from Users u join Business b on u.User_Id=b.User_Id",
                        map:(ub,u,b)=>
                        {
                            ub.UserDetails=u;
                            ub.BusinessDetails=b;
                            return ub;
                        },
                        splitOn:"UserId,BusinessId"
                        );


Here i want to split the result right from beginning to assign data to sub classes

ArunPratap
  • 4,816
  • 7
  • 25
  • 43
Ambareesh
  • 81
  • 1
  • 8

1 Answers1

1

I have removed the first parameter which holds the value and declared as new instance inside map function and got the result


var userDetails = await dbConnection.QueryAsync<
User,Business,UserAndBusiness>(@"
                        select User_Id UserId,User_Name UserName,Business_Id BusinessId,
                        Business_Name BusinessName from Users u join Business b on u.User_Id=b.User_Id",
                        map:(u,b)=>
                        {
                            UserAndBusiness ub = new UserAndBusiness();
                            ub.UserDetails=u;
                            ub.BusinessDetails=b;
                            return ub;
                        },
                        splitOn:"BusinessId"
                        );




Ambareesh
  • 81
  • 1
  • 8