diff --git a/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs b/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs index d2328cc9b..4017c639c 100644 --- a/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs +++ b/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs @@ -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 _logger; // 在控制台 //public static readonly ILoggerFactory MyLoggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); }); // 调试窗口 public static readonly ILoggerFactory MyLoggerFactory = LoggerFactory.Create(builder => { builder.AddDebug(); }); - public IRaCISDBContext(DbContextOptions options, IUserInfo userInfo + public IRaCISDBContext(DbContextOptions options, IUserInfo userInfo, ILogger 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 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 { get; set; } diff --git a/IRaCIS.Core.Infrastructure/_IRaCIS/Exception/CustomException.cs b/IRaCIS.Core.Infrastructure/_IRaCIS/Exception/BusinessValidationFailedException.cs similarity index 100% rename from IRaCIS.Core.Infrastructure/_IRaCIS/Exception/CustomException.cs rename to IRaCIS.Core.Infrastructure/_IRaCIS/Exception/BusinessValidationFailedException.cs diff --git a/IRaCIS.Core.Infrastructure/_IRaCIS/Exception/DBSaveFailedException.cs b/IRaCIS.Core.Infrastructure/_IRaCIS/Exception/DBSaveFailedException.cs new file mode 100644 index 000000000..2efed71b4 --- /dev/null +++ b/IRaCIS.Core.Infrastructure/_IRaCIS/Exception/DBSaveFailedException.cs @@ -0,0 +1,21 @@ +using System; + + +namespace IRaCIS.Core.Infrastructure + +{ + + + public class DBSaveFailedException : Exception + { + + public DBSaveFailedException() + { + + } + + public DBSaveFailedException(string message) : base(message) + { + } + } +}