【发布时间】:2009-10-15 20:42:57
【问题描述】:
我刚开始使用 NHibernate。我使用 Products 和 Suppliers 设置了一个简单的多对多场景,如下所示:
<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
但是,当我尝试将供应商添加到产品并调用 save 时,我收到以下错误
无法将“NHibernate.Collection.PersistentBag”类型的对象转换为“Iesi.Collections.HashedSet”类型
我尝试过使用各种类型,例如 ICollection 和 List(Of T),但我不断收到相同的错误。
无法转换类型为“NHibernate.Collection.Generic.PersistentGenericBag1[Domain.Supplier]' to type 'System.Collections.Generic.List1[Domain.Supplier]”的对象
我在这里错过了什么?
【问题讨论】:
标签: .net nhibernate