I am trying to delete a child object from an aggregate root but I keep receiving an error saying the foreign key is null.
I am using EF and the repository pattern and saving my aggregate root like below.
Code:
Dim product As New ProductInfo
product = _productRepository.FindBy(1).FirstOrDefault
If (product IsNot Nothing) Then
stockTransfer.Remove(product.StockKeys.Where(Function(x) x.Id = 1).FirstOrDefault)
End If
_stockTransferRepository.Save(stockTransfer)
I have read the following two links.
http://blogs.msdn.com/b/dsimmons/arc...ps-in-ef4.aspx
http://mocella.blogspot.com/2010/01/...ect-graph.html
The links suggest to use DeleteObject to manually delete the child from the parent. Because when I remove the child from the parents collection it just removes the relationship but still leaves the record in the database.
My Save method in the repository is expecting an aggregate root. Will I need to create a new Save method inside my repository that passes in the aggregate root and the child object and then delete the child object manually while saving the aggregate root like below.
Code:
Public Sub SaveParentAndRemoveChild(entity As StockTransfer, item As StockTransferItem) Implements IStockTransferRepository.SaveParentAndRemoveChild
GetObjectContext().DeleteObject(item)
MyBase.Save(entity)
End Sub
Any suggestions would be great.
Mike