Database.BeginTransaction() Entity framework rollback throws exception

4.6k views Asked by At

I am using EF6 in my asp.net application. In some code block i am using Transaction Scope. Following is my code

 using (VerbaTrackEntities dataContext = new VerbaTrackEntities())
        {
            TBL_TARGET target = dataContext.TBL_TARGET.Where(x => x.LNG_TARGET_ID == TargetID).SingleOrDefault();
            if (target != null)
            {
                using (var trans = dataContext.Database.BeginTransaction())
                {
                    try
                    {
                        System.Data.Entity.Core.Objects.ObjectContext oc = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)dataContext).ObjectContext;
                        foreach (var Targets in target.TBL_CASE_TARGET.ToList())
                        {
                            oc.DeleteObject(Targets);
                        }
                        foreach (var Targets in target.TBL_USER_TARGET.ToList())
                        {
                            oc.DeleteObject(Targets);
                        }
                        foreach (var Targets in target.TBL_FENCE_TARGET.ToList())
                        {
                            oc.DeleteObject(Targets);
                        }
                        foreach (var Targets in target.TBL_TARGET_COMM.ToList())
                        {
                            oc.DeleteObject(Targets);
                        }
                        foreach (var Targets in target.TBL_TRG_FENCE_STATUS.ToList())
                        {
                            oc.DeleteObject(Targets);
                        }
                        foreach (var Targets in target.TBL_TRG_MOVEMENT.ToList())
                        {
                            oc.DeleteObject(Targets);
                        }
                        TBL_IMAGE deleteImage = dataContext.TBL_IMAGE.Where(s => s.LNG_REF_ID == target.LNG_TARGET_ID && s.INT_TYPE_OF_IMAGE == 2).SingleOrDefault();
                        if (deleteImage != null)
                            oc.DeleteObject(deleteImage);
                        oc.SaveChanges();
                        dataContext.TBL_TARGET.Remove(target);
                        TargetEditForm.UpdateTargetSession(target, 1);
                        trans.Commit();
                        return dataContext.SaveChanges();
                    }
                    catch
                    {
                        trans.Rollback();
                        return 0;
                    }
                }
            }
        }

The following code when fails on trans.Commit();. It goes in catch block for executing trans.Rollback. Here it throws exception Underlying data cannot be rollback. On debug i found that trans.Connection is null. What is correct way to rollback from here ..

1

There are 1 answers

3
Marc Cals On

The trans.Rollback on the catch is not necessary, because the transaction is inside using. When Disposable() from transaction is called, it does automatically a rollback.