2015-12-05 30 views
1

我在解決一個多到很多數據庫設計與關係對象在ServiceStack OrmLite外鍵對象HashSet的,但我需要確保不會有重複。我希望我可以將相關對象的集合定義爲HashSet,但ServiceStack不喜歡它。這裏是我的波蘇斯的簡化版本,我用OrmLite:使用帶有SQL Server

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; } 
} 

Foo只能有唯一Bar s的有關它Option秒。從C#角度來看,A HashSet非常適合這種情況,但ServiceStack正在尋找HashSet類型上的相關實體Id。

有什麼辦法,我可以做到這一點,不會讓ServiceStack嚇壞了?

回答

1

引用類型的屬性需要一個列表,你仍然可以有HashSet的屬性,但它不能是一個參考性質,即它就會與POCO blobbed。

+0

HashSet應該也很棒,你不覺得嗎? – labilbe

+1

@labilbe HashSet是無序的,爲了斷言相等,每個類型都需要覆蓋相等的成員,這很可能是很少使用的,也是錯誤的根源。保存之前只需聲明List唯一性會更容易。 – mythz