Looks like your problem might be that you're creating new objects when you should not be. When you look for the address by AddressCustomerID
, if one is found the primary key, AddressID
, will be populated. So when the SetAddressInfo()
method is called it already knows the AddressID
has a value so it will perform an update with those values you populate in those fields. If there is no AddressID
, it will perform an insert.
So try something like this:
// see if address exists
AddressInfo address = AddressInfoProvider.GetAddresses() .WhereEquals("AddressCustomerID", customerID) .FirstOrDefault();
if (address == null)
{
// set the values
address = new AddressInfo();
address.AddressName = "Address1";
address.customerID = customerID;
address.AddressLine1 = userDto.AddressLine;
...
}
else
{
// assuming the address was found
address.AddressLine1 = userDto.AddressLine;
address.SetValue("Email", userDto.Email);
address.SetValue("CompanyName", userDto.Company);
...
}
AddressInfoProvider.SetAddressInfo(address);
Continue to use the same address
object throughout the process vs. creating new ones.