我剛開始使用NHibernate。我設置一個簡單的多對許多使用產品和供應商如下場景:NHibernate的多對多域對象屬性
<class name="Product" table="Products">
<id name="Id">
<generator class="guid" />
</id>
<property name="Name" />
<bag name="SuppliedBy" table="ProductSuppliers" lazy="true">
<key column="ProductId" foreign-key="FK_ProductSuppliers_ProductId" />
<many-to-many column="SupplierId" class="Supplier" />
</bag>
</class>
<class name="Supplier" table="Suppliers">
<id name="Id">
<generator class="guid" />
</id>
<property name="Name" />
<bag name="Products" table="ProductSuppliers" lazy="true" inverse="true">
<key column="SupplierId" foreign-key="FK_ProductSuppliers_SupplierId" />
<many-to-many column="ProductId" class="Product" />
</bag>
</class>
現在我試圖連線袋到了我的域對象。從閱讀的文檔,我想出了(使用Iesi.Collections LIB):
'In Product
Private _Suppliers As ISet = New HashedSet()
Public Overridable Property SuppliedBy() As HashedSet
Get
Return _Suppliers
End Get
Set(ByVal value As HashedSet)
_Suppliers = value
End Set
End Property
'In Supplier
Private _Products As ISet = New HashedSet()
Public Overridable Property Products() As HashedSet
Get
Return _Products
End Get
Set(ByVal value As HashedSet)
_Products = value
End Set
End Property
然而,當我嘗試和供應商添加到產品,並呼籲救我收到以下錯誤
無法轉換對象的類型'NHibernate.Collection.PersistentBag'鍵入'Iesi.Collections.HashedSet
我試過使用各種類型,例如,ICollection和列表(T),但我不斷收到相應的錯誤。
無法投類型的對象NHibernate.Collection.Generic.PersistentGenericBag 1[Domain.Supplier]' to type 'System.Collections.Generic.List
1 Domain.Supplier]
缺少什麼我在這裏?
非常感謝完美的作品! – 2009-10-16 16:01:23