Using SauceDB, how do I turn a one-to-many relationship, between say table "A" and table "B" respectively, into a list property (containing B objects) in the class that corresponds to table A? The relationship is represented by a foreign key in table B referring to table A (so that many B records can belong to one A record).
2 Answers
I found that I could use the AdditionalInit attribute in order to define a hook which gets called as a database object gets initialized. Since this hook can accept a data store, I can deduce the one-to-many relationship right there.
Here's the relevant excerpt of class A:
public class A
{
...
public List<B> Bs { get; private set; }
[AdditionalInit]
public void OnInit(IDataStore dstore)
{
Bs = dstore.Query<B>().Where(b => b.A.Id == Id).ToList();
}
}
Bear in mind I haven't been able to test this code yet.

- 65,625
- 67
- 195
- 317
Sauce does not support Linq2SQL style navigation properties. However, there are two supported ways to work around this depending on your requirements.
1) Just do the join in your code
IDataStore dstore = .GetDataStore();
var query = from i in dstore.Query<MyTable>()
join x in dstore.Query<MyTable>() on i.Name equals x.Name
select new { };
2) Another way to do it is as follows, and gives a more Navigation Property Style use. Modify your object definition to contain a list and use an [AdditionalInit]
public class Foo
{
public int ID { get; set; }
public string Name { get; set; }
[IgnoredField]
public List<Bar> Bars { get; set; }
[AdditionalInit]
private void LoadBars(IDataStore dstore)
{
Bars = dstore.Query<Bar>().Where(r=> r.Foo = this.ID).ToList();
}
}
That should do what you seek, if you have any more questions let me know.

- 6,798
- 1
- 24
- 51
-
Number 2) is what I did in the meantime :) – aknuds1 Aug 18 '11 at 12:02
-
Ah, I found out I needed the [IgnoredField] attribute, which you include here. – aknuds1 Aug 18 '11 at 12:06