110,566
社区成员
发帖
与我相关
我的任务
分享
public void DeleteItem(long itemId)
{
using (var context = new AppDbContext())
using (var trans = context.Database.BeginTransaction(IsolationLevel.ReadCommitted))
{
//使用中的单件不允许删除
var detail = context.in_changedetail.FirstOrDefault(c => c.ItemId == itemId);
if (detail != null) throw new ShowErrorException("此单件已有出入库记录,不能删除!");
//删除
context.ba_item.Where(c => c.ItemId == itemId).Delete();
context.ba_itemcustom.Where(c => c.ItemId == itemId).Delete();
trans.Commit();
}
}
using (var context = new AppDbContext())
using (var trans = new MyTransaction( context.Database.BeginTransaction(IsolationLevel.ReadCommitted)))
{
//使用中的单件不允许删除
var detail = context.in_changedetail.FirstOrDefault(c => c.ItemId == itemId);
if (detail != null) throw new ShowErrorException("此单件已有出入库记录,不能删除!");
//删除
context.ba_item.Where(c => c.ItemId == itemId).Delete();
context.ba_itemcustom.Where(c => c.ItemId == itemId).Delete();
trans.Commit();
}
但 SQL 命令字 Rollback 是一定会有的,虽然他可能是拼装而成,或放在资源中的
是说,在mysql提供的类库的某处,有rollback指令的代码,只是我没找到?SET AUTOCOMMIT=0 # 关闭自动提交
START TRANSACTION | BEGIN [WORK] #开始一个事务
....... # 可回滚的 SQL 指令集合
COMMIT [WORK] [AND [NO] CHAIN] [[NO] RELEASE] #提交事务
ROLLBACK [WORK] [AND [NO] CHAIN] [[NO] RELEASE] #回滚
开始事务后,只要执行 ROLLBACK 指令,就是回滚了
所以并不一定需要你定义的什么 Rollback() 方法,其实 Rollback() 方法中也只是一些执行 ROLLBACK 指令 的代码而已
在其他方法中不去调用 Rollback() 方法,而是直接 执行 ROLLBACK 指令,并不是大逆不道的事情,反而可以减少调用的开销