When a stored credit card profile is deleted by the shopper, any payment record that references the deleted payment profile is not updated. The payment.GatewayPaymentProfileId value is still populated even though that entity ID no longer exists in ac_GatewayPaymentProfiles.
Since there's no foreign key constraint, my only other option seems to be creating a BeforeDelete override in the repository class.
This creates all sorts of problems later in life, especially when something simple like 'if (payment.PaymentProfile != null)' throws an nHibernate exception.
Solution:
So I added this method to the GatewayPaymentProfileRepository.cs file. See below.
My Question:
Is this the recommended way to do what is needed? I can't keep having ac_Payment records pointing to payment profiles that no longer exist. And I don't want to litter up my code with a bunch of try-catch.
Thoughts?
Code: Select all
// BEGIN MOD: AbleMods.com
// DATE: 02/01/2016
/// <inheritdoc />
public override void BeforeDelete(object entity)
{
// Cast the abstract object into a typed instance of the class
GatewayPaymentProfile profile = (GatewayPaymentProfile) entity;
// Remove reference for any payments associates with this payment profile
NHibernateHelper.CreateSQLQuery("UPDATE ac_Payments SET GatewayPaymentProfileId = NULL WHERE GatewayPaymentProfileId = :targetGatewayPaymentProfileId ")
.SetInt32("targetGatewayPaymentProfileId ", profile.Id)
.ExecuteUpdate();
}
// END MOD: AbleMods.com