Search code examples
c#mappingsubclassinsight.database

Insight.Database [BindChildren] on subclass not working


I am using insight.database with my c# project. I Have a class that has a subclass. I am using an inner join in my SP to flatten and get my fields. However, I want some of the query result fields to map to my subclass. It's not working as I expected. I have tried using the BindChildren attribute on both parent and subclass separately, then at the same time, but none of these worked. The parent class maps properly but no values are assigned to the subclass properties. Can someone let me know what I am doing wrong?

SQL (spGetUserById):

select t1.id, t1.FirstName, t1.LastName,t2.Id, t2.GroupName
from table1 t1 
     inner join table2 t2 on t1.groupId = t2.id
where t1.id = @userId

Repository interface:

public interface IUserRepository
{
    [Sql("spGetUserById")]
    User GetUserById(int userid);
}

Parent class:

public class User
{
    public int Id{get;set;}
    public string FirstName{get;set;}
    public string LastName{get; set;}
    public UserGroup Group{get;set;}
}

Sub class :

[BindChildren]
public class UserGroup
{
    public int Id{get;set;}
    public string GroupName{get;set;}
}

Solution

  • since this is a OneToOne mapping and not a parent -> child list mapping, I could have use a Recordset instead of a [BindChildren]:

    public interface IUserRepository
    {
        [Recordset(0, typeof(User), typeof(UserGroup))]
        [Sql("spGetUserById")]
        User GetUserById(int userid);
    }
    

    Solution was posted here: https://github.com/jonwagner/Insight.Database/issues/437