飞客 - EF Core
type: channel
link: https://achmydream.com/tag/EF-Core/
items: DataContext 使用事务

DataContext 使用事务
type: item
date: 2025-09-26T21:23:24+08:00
description: //数据库上下文对象 var dc = new DataContexts(); if (dc.Connection.State == ConnectionState.Closed) { dc.Connection.Open(); } using (var dcTran = dc.Connection.BeginTransaction()) { dc.Transaction = dcTran; try { dc.SubmitChanges(); dcTran.Commit(); // 提交事务 } catch (Exception ex) { dcTran.Rollback(); // 回滚事务 return rp.setError(ex.Message + ex.StackTrace); } }