How to implement Commit and Rollback in LINQ?
To implement transaction(commit and Rollback) concept in LINQ
DbTransaction class and method provide the base of transaction. While inserting record using LINQ if error occur then rollback the record else commit the record.
Take an example UserMst table insertion
DbTransaction tranUserMst=null;
ExampleDataContext() objeDC = new ExampleDataContext();
try
{
objeDC.Connection.Open();
tranUserMst= objeDC.Connection.BeginTransaction();
objeDC.Transaction = tranUserMst;
UserMst objNewUserMst = new UserMst();
objNewUserMst.UserMstID = "MC01";
objNewUserMst.Name = "Muhil";
objeDC.UserMsts.InsertOnSubmit(objNewUserMst);
objeDC.Submitchanges();
tranUserMst.Commit();
}
catch(Exception ex)
{
tranUserMst.Rollback();
}