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.
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)
I have read the following two links.
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.
Public Sub SaveParentAndRemoveChild(entity As StockTransfer, item As StockTransferItem) Implements IStockTransferRepository.SaveParentAndRemoveChild
Any suggestions would be great.