我刚开始使用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