SqlSuger生成数据库异常处理
This commit is contained in:
parent
d9a2eddc3b
commit
3017f198c4
@ -114,6 +114,8 @@ public static class SqlSugarExtensions
|
|||||||
private static readonly Action<SqlSugarClient> Aop = (db) =>
|
private static readonly Action<SqlSugarClient> Aop = (db) =>
|
||||||
{
|
{
|
||||||
db.Aop.DataExecuting = (_, entityInfo) =>
|
db.Aop.DataExecuting = (_, entityInfo) =>
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
switch (entityInfo.OperationType)
|
switch (entityInfo.OperationType)
|
||||||
{
|
{
|
||||||
@ -132,7 +134,11 @@ public static class SqlSugarExtensions
|
|||||||
}
|
}
|
||||||
if (entityInfo.EntityValue is ICreatedUserId { CreatedUserId: 0 } createdUserId)
|
if (entityInfo.EntityValue is ICreatedUserId { CreatedUserId: 0 } createdUserId)
|
||||||
{
|
{
|
||||||
createdUserId.CreatedUserId = App.User.FindFirst(AuthClaimsConst.AuthIdKey)!.Value.Adapt<long>();
|
var claim = App.User?.FindFirst(AuthClaimsConst.AuthIdKey);
|
||||||
|
if (claim != null)
|
||||||
|
{
|
||||||
|
createdUserId.CreatedUserId = claim.Value.Adapt<long>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case DataFilterType.UpdateByObject:
|
case DataFilterType.UpdateByObject:
|
||||||
@ -145,6 +151,17 @@ public static class SqlSugarExtensions
|
|||||||
case DataFilterType.DeleteByObject:
|
case DataFilterType.DeleteByObject:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
catch (TargetInvocationException ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"[Error] TargetInvocationException in DataExecuting: {ex.InnerException?.ToString() ?? ex.Message}");
|
||||||
|
throw ex.InnerException ?? ex;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"[Error] Exception in DataExecuting: {ex.ToString()}");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 文档地址:https://www.donet5.com/Home/Doc?typeId=1204
|
// 文档地址:https://www.donet5.com/Home/Doc?typeId=1204
|
||||||
|
|||||||
@ -28,9 +28,12 @@ public class SqlSugarUnitOfWork : IUnitOfWork
|
|||||||
/// <param name="resultContext"></param>
|
/// <param name="resultContext"></param>
|
||||||
/// <param name="unitOfWork"></param>
|
/// <param name="unitOfWork"></param>
|
||||||
public void CommitTransaction(FilterContext resultContext, UnitOfWorkAttribute unitOfWork)
|
public void CommitTransaction(FilterContext resultContext, UnitOfWorkAttribute unitOfWork)
|
||||||
|
{
|
||||||
|
if (_client.Ado.Transaction != null && _client.Ado.Connection != null && _client.Ado.Connection.State == System.Data.ConnectionState.Open)
|
||||||
{
|
{
|
||||||
_client.Ado.CommitTran();
|
_client.Ado.CommitTran();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 回滚
|
/// 回滚
|
||||||
@ -38,9 +41,12 @@ public class SqlSugarUnitOfWork : IUnitOfWork
|
|||||||
/// <param name="resultContext"></param>
|
/// <param name="resultContext"></param>
|
||||||
/// <param name="unitOfWork"></param>
|
/// <param name="unitOfWork"></param>
|
||||||
public void RollbackTransaction(FilterContext resultContext, UnitOfWorkAttribute unitOfWork)
|
public void RollbackTransaction(FilterContext resultContext, UnitOfWorkAttribute unitOfWork)
|
||||||
|
{
|
||||||
|
if (_client.Ado.Transaction != null && _client.Ado.Connection != null && _client.Ado.Connection.State == System.Data.ConnectionState.Open)
|
||||||
{
|
{
|
||||||
_client.Ado.RollbackTran();
|
_client.Ado.RollbackTran();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 释放连接对象
|
/// 释放连接对象
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user