none
EF Rollback insert sp using ObjectContext RRS feed

  • Question

  • Hello, How can I rollback an inserted values using EF6 + SP with ObjectContext?

    using (MyObjectContext context = new MyObjectContext())
    {
    	try
    	{
    		ObjectParameter iIdParent = new ObjectParameter("PARENT_ID", typeof(int));
    		// EXECUTE INSERT PARENT STORED PROCEDURE
    		var result = context.InsertParent(param1, param2, iIdParent);
    
    		if (condition == true)
    		{
    			// EXECUTE INSERT CHILD STORED PROCEDURE ONLY IF CONDITION IS TRUE
    			context.InsertChild(Convert.ToInt32(iIdPlanilla.Value), param3);
    			context.SaveChanges();
    		}
    		else
    		{
    			// ROLLBACK INSERT PARENT STORED PROCEDURE IF CONDITION IS FALSE ???			
    			sError = "Error condition";
    			throw new Exception(sError);
    		}
    	}
    	catch (Exception ex)
    	{
    		sError = ex.Message;
    	}
    }
    Thanks in advance
    Tuesday, February 20, 2018 6:32 PM

All replies

  • I suggest that you wrap everything in a System.Transaction scope.
    Wednesday, February 21, 2018 5:37 AM
  • Hi Spalenzuela,

    I would suggest that you could write insert parent and child into one stored procedure, and pass condition as a parameter. then you could rollback via stored procedure with the value of condition.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 22, 2018 2:37 AM
    Moderator