Hello,
I have an issue when trying to add an entity to another entity and saving the parent entity. I have three classes, customer, address and contact. Address and Contact have a many to many relationship.
I am using your EntityBase class on all three classes and the Customer class implements IAggregateRoot but the address and contact classes don't.
Below is the code I am using.
Code:
Dim lscustomers As List(Of Customer) = _customerRepository.Find(2).ToList
Dim customerInst As New Customer
If (lscustomers.Count > 0) Then
Dim contactInst As New Contact
contactInst = request.ContactView.ConvertToContact
Dim addressInst As Address = request.AddressView.ConvertToAddress
addressInst.AddContact(contactInst)
customerInst = lscustomers.FirstOrDefault
customerInst.AddAddress(addressInst)
addResponse.BrokenRules = customerInst.GetBrokenRules()
End If
_customerRepository.Save(customerInst)
_uow.CustomerCommit()
When I use the above code I receive the following error message.
Unable to cast object of type 'PDS.PDSOS.Model.Models.Customers.Address' to type 'PDS.PDSOS.Model.Models.Customers.Contact'
But if I do not add the contact object to the address object it then saves correctly.
Can I save multiple levels of objects and only save the top level object *customer* to the DB.
Or do I have to save the address object seperately which has the contact object inside it? So I end up with the following.
_customerRepository.Save(customerInst)
_addressRepository.Save(addressInst)