SqlSuger生成数据库异常处理
This commit is contained in:
parent
d9a2eddc3b
commit
3017f198c4
@ -115,35 +115,52 @@ public static class SqlSugarExtensions
|
|||||||
{
|
{
|
||||||
db.Aop.DataExecuting = (_, entityInfo) =>
|
db.Aop.DataExecuting = (_, entityInfo) =>
|
||||||
{
|
{
|
||||||
switch (entityInfo.OperationType)
|
try
|
||||||
{
|
{
|
||||||
//执行insert时
|
switch (entityInfo.OperationType)
|
||||||
case DataFilterType.InsertByObject:
|
{
|
||||||
//自动设置主键
|
//执行insert时
|
||||||
if (entityInfo.EntityColumnInfo.IsPrimarykey &&
|
case DataFilterType.InsertByObject:
|
||||||
entityInfo.EntityValue is Entity<long> { Id: 0 } entity)
|
//自动设置主键
|
||||||
{
|
if (entityInfo.EntityColumnInfo.IsPrimarykey &&
|
||||||
entity.Id = YitIdHelper.NextId();
|
entityInfo.EntityValue is Entity<long> { Id: 0 } entity)
|
||||||
}
|
{
|
||||||
//如果当前实体继承ICreatedTime就设置创建时间
|
entity.Id = YitIdHelper.NextId();
|
||||||
if (entityInfo.EntityValue is ICreatedTime createdTime && createdTime.CreatedTime == DateTime.MinValue)
|
}
|
||||||
{
|
//如果当前实体继承ICreatedTime就设置创建时间
|
||||||
createdTime.CreatedTime = DateTime.Now;
|
if (entityInfo.EntityValue is ICreatedTime createdTime && createdTime.CreatedTime == DateTime.MinValue)
|
||||||
}
|
{
|
||||||
if (entityInfo.EntityValue is ICreatedUserId { CreatedUserId: 0 } createdUserId)
|
createdTime.CreatedTime = DateTime.Now;
|
||||||
{
|
}
|
||||||
createdUserId.CreatedUserId = App.User.FindFirst(AuthClaimsConst.AuthIdKey)!.Value.Adapt<long>();
|
if (entityInfo.EntityValue is ICreatedUserId { CreatedUserId: 0 } createdUserId)
|
||||||
}
|
{
|
||||||
break;
|
var claim = App.User?.FindFirst(AuthClaimsConst.AuthIdKey);
|
||||||
case DataFilterType.UpdateByObject:
|
if (claim != null)
|
||||||
if (entityInfo.EntityValue is IUpdatedTime updatedTime)
|
{
|
||||||
{
|
createdUserId.CreatedUserId = claim.Value.Adapt<long>();
|
||||||
updatedTime.UpdatedTime = DateTime.Now;
|
}
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
case DataFilterType.UpdateByObject:
|
||||||
|
if (entityInfo.EntityValue is IUpdatedTime updatedTime)
|
||||||
|
{
|
||||||
|
updatedTime.UpdatedTime = DateTime.Now;
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
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;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -29,7 +29,10 @@ public class SqlSugarUnitOfWork : IUnitOfWork
|
|||||||
/// <param name="unitOfWork"></param>
|
/// <param name="unitOfWork"></param>
|
||||||
public void CommitTransaction(FilterContext resultContext, UnitOfWorkAttribute unitOfWork)
|
public void CommitTransaction(FilterContext resultContext, UnitOfWorkAttribute unitOfWork)
|
||||||
{
|
{
|
||||||
_client.Ado.CommitTran();
|
if (_client.Ado.Transaction != null && _client.Ado.Connection != null && _client.Ado.Connection.State == System.Data.ConnectionState.Open)
|
||||||
|
{
|
||||||
|
_client.Ado.CommitTran();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -39,7 +42,10 @@ public class SqlSugarUnitOfWork : IUnitOfWork
|
|||||||
/// <param name="unitOfWork"></param>
|
/// <param name="unitOfWork"></param>
|
||||||
public void RollbackTransaction(FilterContext resultContext, UnitOfWorkAttribute unitOfWork)
|
public void RollbackTransaction(FilterContext resultContext, UnitOfWorkAttribute unitOfWork)
|
||||||
{
|
{
|
||||||
_client.Ado.RollbackTran();
|
if (_client.Ado.Transaction != null && _client.Ado.Connection != null && _client.Ado.Connection.State == System.Data.ConnectionState.Open)
|
||||||
|
{
|
||||||
|
_client.Ado.RollbackTran();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user