customerContact.PreferredBillingAddress = null, throws NullReferenceException

Vote:
 

In the past I've been able to just do something like this:

customerContact.PreferredBillingAddress = null;
customerContact.PreferredShippingAddress = null;

customerContact.SaveChanges();

But now when trying to set PreferredBillingAddress = null I get NullReferenceException (this happens before calling SaveChanges().

What's the recommended way to remove/reset the PreferredBillingAddress and PreferredShippingAddress? The site is running Episerver Commerce 10.8.0.

#184480
Oct 27, 2017 19:24
Vote:
 
customerContact.PreferredBillingAddressId = null;
customerContact.PreferredShippingAddressId = null;
#184481
Oct 27, 2017 20:16
Vote:
 

Thanks Mark. I had tried that and it threw a foreign key exception, but the reason was that I was traversing through all the contact addresses and deleting them as well. Once I split it up, it worked:

customerContact.PreferredBillingAddressId = null;
customerContact.PreferredShippingAddressId = null;

customerContact.SaveChanges();

foreach (var customerAddress in customerContact.ContactAddresses)
{               
    customerContact.DeleteContactAddress(customerAddress);
}

customerContact.SaveChanges();

Thanks

Frederik

#184483
Oct 27, 2017 21:04
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.