增加异常处理
parent
779b07b1b5
commit
d995cc01bc
|
@ -17,28 +17,32 @@ using Microsoft.EntityFrameworkCore.ValueGeneration;
|
|||
using UserTypeGroup = IRaCIS.Core.Domain.Models.UserTypeGroup;
|
||||
using IRaCIS.Core.Infra.EFCore.Common;
|
||||
using IRaCIS.Core.Infra.EFCore.Common.Dto;
|
||||
using Microsoft.Identity.Client;
|
||||
using EntityFramework.Exceptions.Common;
|
||||
using System.Data;
|
||||
using IRaCIS.Core.Infrastructure;
|
||||
|
||||
namespace IRaCIS.Core.Infra.EFCore
|
||||
{
|
||||
public class IRaCISDBContext : DbContext
|
||||
{
|
||||
public readonly IUserInfo _userInfo;
|
||||
//private readonly IAuditingData _auditingData;
|
||||
|
||||
public readonly ILogger<IRaCISDBContext> _logger;
|
||||
// 在控制台
|
||||
//public static readonly ILoggerFactory MyLoggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); });
|
||||
// 调试窗口
|
||||
public static readonly ILoggerFactory MyLoggerFactory = LoggerFactory.Create(builder => { builder.AddDebug(); });
|
||||
|
||||
public IRaCISDBContext(DbContextOptions<IRaCISDBContext> options, IUserInfo userInfo
|
||||
public IRaCISDBContext(DbContextOptions<IRaCISDBContext> options, IUserInfo userInfo, ILogger<IRaCISDBContext> logger
|
||||
|
||||
|
||||
//IAuditingData auditingData
|
||||
//IAuditingData auditingData
|
||||
) : base(options)
|
||||
{
|
||||
_logger= logger;
|
||||
_userInfo = userInfo;
|
||||
//this._auditingData = auditingData;
|
||||
//_configuration = configuration;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -496,12 +500,64 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
return base.SaveChanges();
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
|
||||
{
|
||||
// 采用触发器的方式 设置 CreateUserId CreateTime UpdateTime UpdateUserId 稽查实体里面没有这四个字段的值 因为先后顺序的原因
|
||||
SetCommonEntityAuditInfo();
|
||||
await AddAudit();
|
||||
return await base.SaveChangesAsync(cancellationToken);
|
||||
|
||||
try
|
||||
{
|
||||
return await base.SaveChangesAsync(cancellationToken);
|
||||
|
||||
}
|
||||
catch (UniqueConstraintException ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
|
||||
throw new DBSaveFailedException("该唯一键已经存在于数据库中。");
|
||||
|
||||
}
|
||||
catch (TimeoutException ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
|
||||
throw new DBSaveFailedException("数据库操作已经超时,请稍后重试。");
|
||||
|
||||
}
|
||||
catch (CannotInsertNullException ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
|
||||
throw new DBSaveFailedException("无法在非空列上插入空值。");
|
||||
}
|
||||
catch (MaxLengthExceededException ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
|
||||
throw new DBSaveFailedException("字符串超过了数据库列的最大长度。");
|
||||
}
|
||||
catch (NumericOverflowException ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
|
||||
throw new DBSaveFailedException("数值超过了数据类型的范围。");
|
||||
}
|
||||
catch (SyntaxErrorException ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
|
||||
throw new DBSaveFailedException("SQL 查询中存在语法错误。");
|
||||
}
|
||||
catch (DbUpdateConcurrencyException ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
|
||||
throw new DBSaveFailedException("SQL 事务失败,请检查环境。");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -670,7 +726,6 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
#endregion
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
public virtual DbSet<TaskAllocationRule> TaskAllocationRule { get; set; }
|
||||
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
using System;
|
||||
|
||||
|
||||
namespace IRaCIS.Core.Infrastructure
|
||||
|
||||
{
|
||||
|
||||
|
||||
public class DBSaveFailedException : Exception
|
||||
{
|
||||
|
||||
public DBSaveFailedException()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public DBSaveFailedException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue