Search code examples
c#servicestackormlite-servicestack

Using a HashSet for foreign key objects in ServiceStack OrmLite with SQL Server


I'm having to work around a many-to-many database design with a relation object, but I need to make sure there aren't duplicates. I had hoped I could just define the collection of related objects as a HashSet, but ServiceStack doesn't like it. Here's a simplified version of my POCOs I use for OrmLite:

public class Foo
{
    public int Id { get; set; }
    [Reference]
    public HashSet<FooToBar> FooToBars { get; set; }
}

public class Bar
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class FooToBar
{
    public int Id { get; set; }

    [References(typeof(Foo))]
    public int FooId { get; set; }
    [References(typeof(Bar))]
    public int BarId { get; set; }

    // This is the requirement that makes it a little odd
    public string Option { get; set; }
}

Class Foo can only have unique Bars with Options related to it. A HashSet is great for this from a C# perspective, but ServiceStack is looking for the related entity Id on the HashSet type.

Is there any way I can accomplish this that won't make ServiceStack freak out?


Solution

  • Reference type properties need to be a List, you can still have HashSet properties but it can't be a reference property, ie it gets blobbed with the POCO.