2010-06-19 91 views
0

我正在使用單軌/ activerecord,並且想知道如何處理多對多關係中的添加項目,當多對多表中除了2個外鍵之外還有一個值。Castle ActiveRecord HasAndBelongsToMany問題

例如,商業和娛樂類有多對多的關係,所以有一個商業滿意度表。如果BusinessAmenity表只用了外鍵BusinessId和AmenityId那麼你可以這樣做:

[HasAndBelongsToMany(typeof(Amenity), 
      Table = "BusinessAmenity", ColumnKey = "businessid", ColumnRef = "amenityid", Cascade = ManyRelationCascadeEnum.None, Lazy=true)] 
     public IList<Amenity> Amenities 
     { 
      get { return _amenities; } 
      set { _amenities = value; } 
     } 

,然後加入協會這樣的:

business.Amenities.Add(amenity; 

但是,如果什麼BusinessAmenity類有另一列所謂的「價值」,需要爲每個協會設置?您不能再向Business.Amenities添加Amenity對象,因爲您需要能夠在BusinessAmenity中設置Value屬性。

有人可以提供一些洞察你如何在ActiveRecord中做到這一點?

謝謝! 賈斯汀

回答

2

地圖的BusinessAmenity自身的BusinessAmenity類,如(僞):

[ActiveRecord] 
class Business { 
    [PrimaryKey] int Id {get;set;} 
    [HasMany] ISet<BusinessAmenity> Amenities {get;set;} 
} 

[ActiveRecord]  
class Amenity { 
    [PrimaryKey] int Id {get;set;} 
    [HasMany] ISet<BusinessAmenity> Businesses {get;set;} 
} 

[ActiveRecord]  
class BusinessAmenity { 
    [BelongsTo] Amenity Amenity {get;set;} 
    [BelongsTo] Business Business {get;set;} 
    [Property] int Value {get;set;} 
} 

這一問題已經在計算器討論過很多次: