7/5/2025 6:30:00 AM
|
|
slxdeveloper.com Community Forums |
|
|
|
The Forums on slxdeveloper.com are now retired. The forum archive will remain available for the time being. Thank you for your participation on slxdeveloper.com!
Forum to discuss the use of the SalesLogix Web Platform, Client and Customer Portals, and the Application Architect (For version 7.2 and higher only). View the code of conduct for posting guidelines.
|
|
|
|
Cascading delete conflict
Posted: 20 Aug 09 12:42 AM
|
I have a custom entity AccountService that has a child relationship with the Account entity with a cascading delete. The AccountService entity has an OnBeforeDelete event with code that updates a field in Account, like this:
public static void OnBeforeDeleteStep( IAccountService accountservice, ISession session) { IAccount acc = accountservice.Account; if (acc != null) { acc.UpdateLeadscore(); session.Save(acc); } }
The problem is that if I delete the Account entity, this AccountService entity OnBeforeDelete event will try to update the Account entity causing the following error:
An exception occurred executing the dynamic method AccountService.OnBeforeDelete.: deleted object would be re-saved by cascade (remove deleted object from associations)
Does anyone know how I can detect in the event code above when the Account is in the process of being deleted? I guess I could write code to delete the associated AccountServices collection in the OnBeforeDelete event of the Account and turn off the cascading delete for the relationship. Or is there a better way to handle this?
Thanks! Doug |
|
|
|
Re: Cascading delete conflict
Posted: 21 Aug 09 11:36 AM
|
Doug - you always come up with the hard ones!
Try dissociating the account service at this point: accontservice.Account.accountservices.Remove(accountservice);
This will prevent the cascading delete.
|
|
|
|
Re: Cascading delete conflict
Posted: 21 Aug 09 1:29 PM
|
Thanks Mike - I'm not looking for trouble, honest!
I will try your suggestion. If it works I think it will be preferable to what I ended up doing.
I turned off cascading delete for the AccountService relationship with Account and added the following Post Execute Step to the OnBeforeDelete event on Account:
public static void OnBeforeDeleteStep( IAccount account, ISession session) { try { IDbCommand command = session.Connection.CreateCommand(); session.Transaction.Enlist(command); string str = "DELETE FROM RSI_ACCOUNTSERVICE WHERE ACCOUNTID ='" + account.Id + "'"; command.CommandText = str; command.ExecuteNonQuery(); } catch (Exception exception) { session.Transaction.Rollback(); throw new Exception(string.Format("An error occurred while deleting services for the account.", account.AccountName, account.Id), exception.InnerException); } }
This seems to be working but feels wrong to circumvent the ORM this way. It's based on the code for the standard OnBeforeDelete event for Account which can be seen using .NET Reflector. Any idea why the supplied code for this event deletes some entities (mostly related to Activity) using this method and not others (such as Opportunity and Contact)? As far as I can tell associated Opportunities and Contacts (for example) are deleted by NHibernate as part of the cascade logic defined in the relationship.
-Doug |
|
|
|
You can
subscribe to receive a daily forum digest in your
user profile. View the site code
of conduct for posting guidelines.
Forum RSS Feed - Subscribe to the forum RSS feed to keep on top of the latest forum activity!
|
|
|
|
|
|
|
|