Fluent nHibernate でマッピングしようとして失敗する、少し奇妙な状況が発生しました。Image オブジェクトと File オブジェクトの両方を含む Asset オブジェクトがあります。Image と File の ID は同じで、Image オブジェクトには File オブジェクトが含まれています。この状況が発生するのは、イメージは常にファイルでもありますが (ID が一致する必要があるのはそのためです)、ファイルは常にイメージであるとは限りません。
これを次のようにマッピングしました。
アセットマップ
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().Cascade.All()
References(Function(x) x.File).LazyLoad().Cascade.All()
References(Function(x) x.Row).Cascade().All()
Map(Function(x) x.AssetType).CustomType(Of AssetType)()
End Sub
イメージマップ
Public Sub New()
Id(Function(x) x.ID)
Map(Function(x) x.Height)
Map(Function(x) x.Width)
Map(Function(x) x.AltText)
Map(Function(x) x.ToolTip)
Map(Function(x) x.ImageStatus).CustomType(Of ImageStatus)()
References(Function(x) x.Product).Nullable()
HasOne(Function(x) x.File).Constrained()
References(Function(x) x.ViewTag)
HasManyToMany(Function(x As Image) x.ProductOptionValues).Table("ImageVsProductOptionValues").LazyLoad().Cascade.All()
HasManyToMany(Function(x As Image) x.MappedCategories).Table("CategoryVsImage").LazyLoad().Cascade.All().Inverse()
End Sub
ファイルマップ
Public Sub New()
Id(Function(x) x.Id)
Map(Function(x) x.Data).LazyLoad().Length(Integer.MaxValue)
Map(Function(x) x.MimeType)
Map(Function(x) x.Size)
Map(Function(x) x.Filename)
Map(Function(x) x.LastDateModified)
Map(Function(x) x.DateCreated)
End Sub
次のコードを使用して新しい画像を作成し、アセットに追加して保存しようとすると、問題が発生しました。
If oAsset.Image Is Nothing Then
currentImage = New CMS.DataTransferObjects.Image
currentFile = New CMS.DataTransferObjects.File
Else
currentImage = oAsset.Image
currentFile = oAsset.File
End If
currentFile.Data = ms.ToArray
currentFile.MimeType = mimeType
currentFile.Filename = filImgUpload.FileName
currentFile.Size = filImgUpload.ContentLength
currentImage.Width = CInt(Utils.Convert.ToInt64(UploadedImage.PhysicalDimension.Width))
currentImage.Height = CInt(Utils.Convert.ToInt64(UploadedImage.PhysicalDimension.Height))
If oAsset.Image Is Nothing Then
oAsset.Image = currentImage
oAsset.File = currentFile
Else
'currentImage = oAsset.Image
'currentFile = oAsset.File
End If
次に、nHibernate マネージャーを呼び出して、アセットを更新しようとすると、次のエラーが発生します。
The INSERT statement conflicted with the FOREIGN KEY constraint "FK30EBACDFED57EBE9". The conflict occurred in database "BDM1_TestBed", table "dbo.File", column 'Id'.
誰でもこの混乱を整理するのを手伝ってもらえますか - 私のマッピングが間違っていると思いますが、改善する方法がわかりませんか?