locked
Lightswitch SaveChanges() and TargetInvokationException RRS feed

  • Question

  • I've been having this exception recently, and couldn't find why. I found this article that described a very similar situation, but it didn't get to a solution. Then I found this report that resulted on a bug which was closed as "won't fix"; however, it gave me an idea on what it might be happening, and I was able to find and fix what was causing the exception.

    I still think this is a bug and should be fixed maybe by notifying during debugging or tooltips that certain actions may result on TargetInvokationException when you modify a parent's entity data during saving operations for a child entity, or better descriptions for TargetInvokationException.


    Sunday, August 21, 2016 5:02 PM

All replies

  • Hi Daniel,

    Thanks for raising this issue to us through the forums.

    We actively monitor forum conversations in an effort to receive feedback that helps continually improve products.

    Since we can't reproduce your issue on my side, you can provide us with your scenario below, you could also consider submitting this issue to Microsoft Connect Site, Microsoft engineers will reproduce this issue according to the proof you provide, and they will evaluate this issue seriously, then they will give quick and positive response for you.

    Regards,

    Angie


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, August 22, 2016 9:16 AM
  • Hi Angie, these are the repro steps for my case (another example was provided, analyzed, and entered as a bug in the link to the report I mentioned earlier):

    This happens when you modify data of a parent's record within the Me.DataWorkspace.CPMSPartnersOffersData.SaveChanges() code for a child's record. In my case, on a BUTTON_Execute() code I create a new child record thru code and call the SaveChanges()… so, in the Validate() code for the child's entity I process related data (i.e. in my case a call to a 3<sup>rd</sup> Party API POST, and then a call to a StoreProcedure in my Azure SQL DB), and then I was setting the STATUS value of the parent record… this produced the exception, because I was modifying the parent's record data from within the child's Validate() code… However, the entirety of the Validate() code run as expected, no errors whatsoever… the exception come up when the execution went back to the BUTTON's code to complete the SaveChanges() call.

            Private Sub MakeEventLive_Execute()

                     'Create the NewOffer record that'll trigger the API POST call to ACTIVATE the Event

                    Dim MyNewOffer = Me.DataWorkspace.CPMSPartnersOffersData.NewOffers.AddNew

                    MyNewOffer.Operation = "ACTIVATE"

                    MyNewOffer.Offer = Me.Offer   '≪≪==== reference to the parent record

     

                    Me.DataWorkspace.CPMSPartnersOffersData.SaveChanges()   '≪≪==== this is where the exception was raised

             End Sub

    The SaveChanges() for MyNewOffer table would execute the Validate() code for that entity:

            Private Sub NewOffers_Validate(entity As NewOffer, results As EntitySetValidationResultsBuilder)

                'Here I had code to call a Class that will make POST API calls to some related app

                entity.Offer.Status = "Inactive"    '≪≪==== here I'm modifying the parent's data field

                'Then I had some additional code to execute a StoreProc

            End Sub

    To solve the issue, I had to remove the modification of the parent's Status from the Validate() code, and move it up into the Button's code:

            Private Sub MakeEventLive_Execute()

                    'Create the NewOffer record that'll trigger the API POST call to ACTIVATE the Event

                    Dim MyNewOffer = Me.DataWorkspace.CPMSPartnersOffersData.NewOffers.AddNew

                    MyNewOffer.Operation = "ACTIVATE"

                    MyNewOffer.Offer = Me.Offer

                    Me.DataWorkspace.CPMSPartnersOffersData.SaveChanges()

                    Me.Offer.Status = "Active"

            End Sub


    Monday, August 22, 2016 5:32 PM
  • Hi Daniel,

    I have submit this issue to Visual Studio Connect Site, see: Lightswitch SaveChanges() and TargetInvokationException

    Thanks for your understanding.

    Best regards,

    Angie


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, September 16, 2016 9:49 AM