首页 > 解决方案 > 子类上的 Insight.Database [BindChildren] 不起作用

问题描述

我正在使用insight.database我的 c# 项目。我有一个有子类的类。我在我的 SP 中使用内部连接来展平并获取我的字段。但是,我希望一些查询结果字段映射到我的子类。它没有像我预期的那样工作。我曾尝试分别在父类和子类上使用 BindChildren 属性,然后同时使用,但这些都不起作用。父类正确映射,但没有为子类属性分配值。有人可以让我知道我做错了什么吗?

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

仓库界面:

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

父类:

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

子类:

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

标签: c#mappingsubclassinsight.database

解决方案


因为这是一个 OneToOne 映射而不是父 -> 子列表映射,所以我可以使用 Recordset 而不是 [BindChildren]:

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

解决方案发布在这里:https ://github.com/jonwagner/Insight.Database/issues/437


推荐阅读