我遇到了一个循环的问题,该循环应该将nHibernate对象添加到现有的nHibernate对象并保存。这里有一整个关系树,有一个Page对象可以有很多Region对象,而这些对象又可以有很多Row对象,可以有很多Asset对象 - 问题出在后三个,我提供了下面的映射: / p>
AssetMap
Public Sub New()
Id(Function(x) x.Id)
Map(Function(x) x.DisplayOrder)
Map(Function(x) x.Text).Length(10000)
Map(Function(x) x.Title)
Map(Function(x) x.Width)
Map(Function(x) x.Height)
References(Function(x) x.Image).LazyLoad()
References(Function(x) x.Row).Cascade().All()
Map(Function(x) x.AssetType).CustomType(Of AssetType)()
End Sub
RowMap
Public Sub New()
Id(Function(x) x.Id)
Map(Function(x) x.CssBackgroundId)
Map(Function(x) x.DisplayOrder)
Map(Function(x) x.RowType).CustomType(Of RowType)()
HasMany(Function(x) x.Assets).OrderBy("DisplayOrder").Cascade.All()
References(Function(x) x.Region).Cascade().All()
End Sub
RegionMap
Public Sub New()
Id(Function(x) x.Id)
References(Function(x) x.RegionTemplate)
HasMany(Function(x) x.Rows).OrderBy("DisplayOrder").Cascade.All()
Map(Function(x) x.RegionId)
References(Function(x) x.Workflow)
References(Function(x) x.Page)
Map(Function(x) x.Ranking)
End Sub
有问题的代码应该遍历一些数据保持对象,创建一个Row对象,然后创建一些Asset对象,将它们添加到Row对象,然后将Row添加到集合中。最后,新填充的Row将添加到Region对象中。
首次开始为区域设置行和资产时,代码可以正常工作。当你必须对它进行更改并且第二次保存时,麻烦就开始了 - 此时nHibernate创建了两个全新的行对象,将现有的Assets附加到它们,并通过留下它们的null Id来孤立这两个旧行。
For Each oAsset As FrontEndService.PageAsset In assetCollection
Dim oRow = New Cms.DataTransferObjects.Row
oRow.Assets = BuildAssets(oAsset, oRow, regionPos, oPage)
oRowCollection.Add(orow)
Next
Private Function BuildAssets(ByVal oAsset As FrontEndService.PageAsset, ByVal oRow As Cms.DataTransferObjects.Row, ByVal RegionPos As Integer, ByRef oPage As Cms.DataTransferObjects.Page) As List(Of Cms.DataTransferObjects.Asset)
Dim oColumnCollection As New List(Of Cms.DataTransferObjects.Asset)
oRow.DisplayOrder = oAsset.pageAssetOrder
oRow.RowType = DirectCast(CInt(oAsset.pageAssetType), Cms.DataTransferObjects.RowType)
Dim noOfColumns As Integer = GetAmountOfCellsByType(oAsset.pageAssetType)
For iColumncount As Int32 = 0 To noOfColumns - 1
Dim oPageColumn = New Cms.DataTransferObjects.Asset
oPageColumn.DisplayOrder = iColumncount + 1
oPageColumn.AssetType = GetAssetType(oAsset.pageAssetType, iColumncount + 1)
oPageColumn.Width = CInt(oPage.Regions(RegionPos).RegionTemplate.Width / noOfColumns)
oColumnCollection.Add(oPageColumn)
Next
Return oColumnCollection
End Function
有人能告诉我我做错了吗?