直接上代码
using (var db = Context)
{
using (var TR = db.Database.BeginTransaction())
{
try
{
GoodsImg model = new GoodsImg();
model.CreateTime = DateTime.Now;
db.GoodsImgs.Add(model);
db.SaveChanges();
下面这一步会直接报错,转到catch 执行Rollback()结束事务
换成正确的执行代码会直接执行Commit()
var list = db.GoodsImgs.ToList();
list[10].CreateTime = DateTime.Now.AddDays(7);
db.SaveChanges();
TR.Commit();
}
catch (Exception)
{
TR.Rollback();
throw;
}
}
}