none
Unable to add object in OnError method

    Question

  • I have a stored proc that inserts a record into the db. It is attached to a table in the entity framework as the function to perform the inserting. In my domain service I can call it from a method that updates another entity and it works.

    Here's an example of what works...

            public void UpdateT_sec_sys(t_sec_sys currentt_sec_sys)
            {
                t_error_msg msg = new t_error_msg();

                msg.msg_id = "";
                msg.error_code = -1;
                msg.error_msg = "test";
                msg.msg_notes = "test";

                this.ObjectContext.t_error_msg.AddObject(msg);

                this.ObjectContext.t_sec_sys.AttachAsModified(currentt_sec_sys, this.ChangeSet.GetOriginal(currentt_sec_sys));
            }

    When I call it from the OnError method the record never gets created.

    Here's what doesn't work...

            protected override void OnError(DomainServiceErrorInfo errorInfo)
            {

                t_error_msg msg = new t_error_msg();

                msg.msg_id = "";
                msg.error_code = -1;
                msg.error_msg = errorInfo.Error.Message;
                msg.msg_notes = errorInfo.Error.InnerException.Message;

                this.ObjectContext.t_error_msg.AddObject(msg);
                

                //base.OnError(errorInfo);
            }

    Any help on this is greatly appreciated,

    Mark

    Monday, April 23, 2012 11:51 AM

Answers

  • It depends on the error and where it came from. For example, the call to OnError may be happening inside a TransactionScope and when that transaction rollsback so does yours. Try the following code:

     

    using (TransactionScope errorTrans = new TransactionScope(TransactionScopeOption.Suppress))
    {
        YourObjectContext context = new YourObjectContext;
        //your code as is, but using context instead of  this.ObjectContext
       errorTrans.Complete(); //This is a no-op since the transaction was suppressed. Includes for consistency
    }

    Monday, April 23, 2012 6:40 PM