1.引入相应的命名空间

using System.Transactions;

2.代码事例(using (TransactionScope ts = new TransactionScope()))

 public JsonResult DriverCommentAdd(OrderComment orderComment)
{ try
{
using (TransactionScope ts = new TransactionScope())
{
var order = db.Orders.SingleOrDefault(p => p.ID == orderComment.OrderID);
if (order.OrderStatus == (byte)OrderStatus.Commanded
|| order.OrderStatus==(byte)OrderStatus.DriverCommanded)
{
return Json(new { state = true, msg = "该订单已经评论过了" }, JsonRequestBehavior.AllowGet);
} //获取车主信息
var user = authMag.GetUser(Token);
orderComment.FromID = user.ID;
orderComment.ToID = order.PassengerUserID;
orderComment.CreateTime = DateTime.Now;
orderComment.IsCommentByPassgenger = false;
db.Entry<OrderComment>(orderComment).State = EntityState.Added;
if (order.OrderStatus == (byte)OrderStatus.Arrived)
{
order.OrderStatus = (byte)OrderStatus.DriverCommanded;
}
else if (order.OrderStatus == (byte)OrderStatus.PassengerCommanded)
{
order.OrderStatus = (byte)OrderStatus.Commanded;
}
db.SaveChanges();
ts.Complete(); return Json(new { state = true, msg = "评论成功" }, JsonRequestBehavior.AllowGet);
} }
catch (Exception ex)
{
Log.Debug("错误日志:::" + ex.StackTrace);
return Json(new { state = false, msg = "评论失败" + ex.Message }, JsonRequestBehavior.AllowGet);
}
}
04-13 21:21