我有以下(简化)数据模型:
public class Order : IHasId<long>
{
[AutoIncrement]
public long Id { get; set; }
[References(typeof(Material))]
public long MaterialId { get; set; }
[Reference]
public Material Material { get; set; }
}
public class Material : IHasId<long>
{
[AutoIncrement]
public long Id { get; set; }
public string Name { get; set; }
}
我想要实现的是使用Order.Material
引用的材料填充MaterialId
,有没有办法以简单的方式实现这一目标? Load APIs似乎做了类似的事情,但情况相反(当引用位于Material
时,而不是Order
)
答案 0 :(得分:1)
这在OrmLite中称为Self References,适用于您的上述示例:
public class Order : IHasId<long>
{
[AutoIncrement]
public long Id { get; set; }
[References(typeof(Material))]
public long MaterialId { get; set; }
[Reference]
public Material Material { get; set; }
}
public class Material : IHasId<long>
{
[AutoIncrement]
public long Id { get; set; }
public string Name { get; set; }
}
db.Insert(new Material { Name = "A" });
db.Insert(new Material { Name = "B" });
db.Insert(new Order {
MaterialId = 2,
});
var order = db.LoadSingleById<Order>(1);
order.PrintDump();
递归打印对象图到输出:
{
Id: 1,
MaterialId: 2,
Material:
{
Id: 2,
Name: B
}
}