Reputation: 1759
I have the following (simplified) datamodel:
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; }
}
What I want to achieve is to populate Order.Material
with the material referenced by MaterialId
, is there a way to achieve that in a simple way? The Load APIs seem to do something similar, but in the inverse situation (when the reference is on Material
, not on Order
)
Upvotes: 1
Views: 245
Reputation: 143284
This is called Self References in OrmLite and works with your above example:
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();
Recursively print object graph to Output:
{
Id: 1,
MaterialId: 2,
Material:
{
Id: 2,
Name: B
}
}
Upvotes: 1