| | |
| | | |
| | | public ISqlSugarClient Db => BaseDal.Db; |
| | | |
| | | protected async Task<WebResponseContent> ExecuteWithinTransactionAsync(Func<Task<WebResponseContent>> operation) |
| | | { |
| | | var db = Db as SqlSugarClient; |
| | | if (db == null) |
| | | { |
| | | return WebResponseContent.Instance.Error("Database context does not support transactions"); |
| | | } |
| | | |
| | | var ownsTransaction = db.Ado.Transaction == null; |
| | | try |
| | | { |
| | | if (ownsTransaction) |
| | | { |
| | | db.BeginTran(); |
| | | } |
| | | |
| | | var result = await operation(); |
| | | if (result?.Status == true) |
| | | { |
| | | if (ownsTransaction) |
| | | { |
| | | db.CommitTran(); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | if (ownsTransaction) |
| | | { |
| | | db.RollbackTran(); |
| | | } |
| | | |
| | | return result ?? WebResponseContent.Instance.Error("Transaction failed"); |
| | | } |
| | | catch |
| | | { |
| | | if (ownsTransaction) |
| | | { |
| | | db.RollbackTran(); |
| | | } |
| | | |
| | | throw; |
| | | } |
| | | } |
| | | |
| | | private PropertyInfo[] _propertyInfo { get; set; } = null; |
| | | public PropertyInfo[] TProperties |
| | | { |