Search code examples
c#servicestackormlite-servicestack

ServiceStack Ormlite Reference Attribute For Same Type


In ServiceStack OrmLite, I have a table defined as follows:

public class Foo {
    [PrimaryKey]
    [AutoIncrement]
    public long Id { get; set; }

    [References(typeof(Bar))]
    public long BarAId { get; set; }

    [References(typeof(Bar))]
    public long BarBId { get; set; }

    [Reference]
    public Bar BarA { get; set; }

    [Reference]
    public Bar BarB { get; set; }
}

Where obviously Bar is another table, and BarA and BarB refer to two different entries in the parent table. When I attempt to resolve BarA and BarB, they're the same. Is there any way to do auto referencing with two parent objects of the same type, or am I SOL and just need to do the join manually?

Thanks, Chris


Solution

  • Support for multiple Self References was added in this commit.

    In order to be able to the match the fields with the correct reference, the FK Reference Id has to take the format {ReferenceProperty}Id which you've also done in your example:

    public class Foo 
    {
        ...
        [References(typeof(Bar))]
        public long BarAId { get; set; }
    
        [Reference]
        public Bar BarA { get; set; }
    
        [References(typeof(Bar))]
        public long BarBId { get; set; }
    
        [Reference]
        public Bar BarB { get; set; }
    }
    

    This feature is available from v4.0.33+ that's now available on MyGet.