This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
I have a situation where I want to use a legacy stored proc so save an entity. I understand that you can mark an entity with the CrudProcedure Attribute but then you must use stored procs for all operations. I thought then, that perhaps I could use the OnSaving() hook...
Is it possible to cancel the default insert/update and do my own save instead? Thanks, Barry. |
|
|
Hello Barry, Yes, it is possible to cancel the default insert/update/delete in OnSaving. Just set e.Cancel = true and the entity will be skipped from the normal save. (You may have been told earlier that setting e.Cancel cancelled the entire save operation. Sorry about that; I've now checked, and the Cancel option affects only the entity whose OnSaving is being called. Apologies for the confusion.) I have not tested the specific case of calling a stored procedure within OnSaving, but it should work. You will need to enrol the database command against the current connection and transaction, which you can do using PrepareCommand. So something like this:
One important limitation is that your stored procedure cannot assign an ID to the entity. The ID must be assigned client-side (either using one of the normal identity methods, such as KeyTable, or as a natural key using GeneratedId), and sent to the database through the stored procedure. Another consideration is that LightSpeed batches its own updates. We build a batch of SQL statements, then send the entire batch to the database. By executing a command outside our batch processing, your entity might get saved while LightSpeed is half way through composing a batch, so that some prior entities have not yet been sent to the database (their SQL statements have been composed but their batch is still pending). If your sproc depends on those entities being present (e.g. because there are foreign keys to them) then you could get integrity errors. If this is a consideration for you then you can turn off batching by setting UpdateBatchSize to 1. Please let us know if you run into any problems, and apologies again for the incorrect guidance earlier. |
|