How can I query with relations?

Nov 12, 2008 at 10:18 PM
I have class with a BelongsTo relation. Can I use the same query syntax like normal fieldtype (Find(CK.Column["field"] == value))? When I try this DbEntry throws an ArgumentOutOfBoundsException in DataType Parse(Type t) in DataTypeParser.cs.

Can you please post a sample how to query relations (hasMany, BelongsTo, HasAndBelongsToMany)?
Coordinator
Nov 13, 2008 at 12:37 AM
Sorry, It don't have such query in DbEntry.Net.
If the field is BelongsTo, you can just use: "CK.K["field_id"] == value.id".
Otherwise you should use join table mode for query.
Nov 13, 2008 at 7:28 AM
Could you please give me a sample of the join table mode?
Coordinator
Nov 13, 2008 at 12:57 PM
The join table syntax is not in query syntax, it is an attribute set on object definition. The DbTableAttribute could use to set join table too.

The following code shows two table SampleData and TheAge join on SampleData.Id equals TheAge.Id:

[DbTable("SampleData.Id", "TheAge.Id")]
public class JoinTable1 : IDbObject
{
    [DbColumn("SampleData.Id")] public long Id;
    public string Name;
    public UserRole Role;
    public DateTime JoinDate;
    public int Age;
}

And there is another attribute to set join with more details. This attribute is JoinOnAttribute. This attribute should NOT set with DbTableAttribute together. It has more details like how to compare, which mode of join to use.

Because we can join more than 2 tables, but we can not ensure the order of the attributes we get by using .net reflection, so it has an order argument to tell DbEntry the order of join syntax.

The following code shows 3 tables join by using JoinOnAttribute:

[JoinOn(0, "SampleData.Id", "TheAge.Id", CompareOpration.Equal, JoinMode.Inner)]
[JoinOn(1, "SampleData.Id", "EnumTable.Id", CompareOpration.Equal, JoinMode.Inner)]
public class JoinTable2 : IDbObject
{
    [DbColumn("SampleData.Id")] public long Id;
    [DbColumn("SampleData.Name")] public string Name;
    public UserRole Role;
    public DateTime JoinDate;
    public int Age;
    [DbColumn("EnumTable.Name")] public string EnumName;
}

The query of it  might like the following:

DbEntry.From<JoinTable2>().Where(CK.K["SampleData.Name"] == "tom").Select();