如何通过ServiceStack.OrmLite获取通过中间表关联的所有对象?

时间:2018-06-09 07:51:59

标签: ormlite-servicestack

我是刚刚开始使用ServiceStack.OrmLite的初学者。我有个问题。如何获取通过中间表关联的所有对象?

详情如下:

Public class book
{
    Public int id { get; set; }
    Public string name { get; set; }
    [Reference]
    Public List<bookusers> bookusers { get; set; }
}

Public class bookusers
{
    Public int id { get; set; }
    Public int bookid { get; set; }
    Public int userid { get; set; }
    [Reference]
    Public book book { get; set; }
    [Reference]
    Public user userObject { get; set; }
}

Public class user
{
    Public int id { get; set; }
    Public int age { get; set; }
    [Reference]
    Public List<bookusers> userbooks { get; set; }
}
var model = db.LoadSingleById<book>(id);
db.LoadReferences(model);
// model.bookusers[0].userObject is null

1 个答案:

答案 0 :(得分:0)

您无法直接将第二级引用检索为stated in documentation

  

仅加载相关数据1-reference-level deep

快速而肮脏的工作方法可能是下一个

var model = db.LoadSingleById<book>(id);
if (model.bookusers != null && model.bookusers.Any())
{
    foreach (var bookUser in model.bookusers)
    {
        db.LoadReferences(bookUser);
    }
}

然后您应该填充userObject属性。