.Net MVC架构实现事务回滚处理
1.首先我们需要 添加引用:using System.Transactions;
2.实现事务回滚
注:事务回滚的要用using (TransactionScope sc = new TransactionScope()){}
using System.Transactions;
public ActionResult Index()
{
using (TransactionScope sc = new TransactionScope())
{
try
{
using (DBEntities db = new DBEntities())
{
WorkTime wt = new WorkTime() { DayTime=DateTime.Now,LongTime=12 };
db.WorkTime.Add(wt);
db.SaveChanges();
}
sc.Complete();//一定要放在catch上面,否则不能回滚
}
catch (Exception)
{
}
}
return View();
}