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