diff --git a/IRaCIS.Core.API/_ServiceExtensions/Swagger/SwaggerSetup.cs b/IRaCIS.Core.API/_ServiceExtensions/Swagger/SwaggerSetup.cs index 56da182f..68905d6c 100644 --- a/IRaCIS.Core.API/_ServiceExtensions/Swagger/SwaggerSetup.cs +++ b/IRaCIS.Core.API/_ServiceExtensions/Swagger/SwaggerSetup.cs @@ -108,17 +108,17 @@ namespace IRaCIS.Core.API options.SwaggerEndpoint($"swagger/Management/swagger.json", "管理模块"); options.SwaggerEndpoint($"swagger/Image/swagger.json", "影像模块"); options.SwaggerEndpoint($"swagger/Reading/swagger.json", "读片模块"); - + //路径配置,设置为空,表示直接在根域名(localhost:8001)访问该文件, //注意localhost:8001/swagger是访问不到的,去launchSettings.json把launchUrl去掉,如果你想换一个路径,直接写名字即可,比如直接写c.Route = "doc"; //options.RoutePrefix = string.Empty; - - //options.IndexStream = () => Assembly.GetExecutingAssembly() - //.GetManifestResourceStream("IRaCIS.Core.API.wwwroot.swagger.ui.Index.html"); + + options.IndexStream = () => Assembly.GetExecutingAssembly() + .GetManifestResourceStream("IRaCIS.Core.API.wwwroot.swagger.ui.Index.html"); options.RoutePrefix = string.Empty; diff --git a/IRaCIS.Core.Application/BusinessFilter/ProjectExceptionFilter.cs b/IRaCIS.Core.Application/BusinessFilter/ProjectExceptionFilter.cs index 57ca1709..7aef3c0d 100644 --- a/IRaCIS.Core.Application/BusinessFilter/ProjectExceptionFilter.cs +++ b/IRaCIS.Core.Application/BusinessFilter/ProjectExceptionFilter.cs @@ -25,7 +25,7 @@ namespace IRaCIS.Core.Application.Filter context.Result = new JsonResult(ResponseOutput.NotOk("并发更新,当前不允许该操作" + context.Exception.Message)); } - if (context.Exception.GetType() == typeof(BusinessValidationFailedException)) + if (context.Exception.GetType() == typeof(BusinessValidationFailedException) || context.Exception.GetType() == typeof(DBSaveFailedException)) { context.Result = new JsonResult(ResponseOutput.NotOk(context.Exception.Message,ApiResponseCodeEnum.BusinessValidationFailed)); } diff --git a/IRaCIS.Core.Infra.EFCore/Common/ReadingCommon.cs b/IRaCIS.Core.Infra.EFCore/Common/ReadingCommon.cs index e9a47214..7d5d0184 100644 --- a/IRaCIS.Core.Infra.EFCore/Common/ReadingCommon.cs +++ b/IRaCIS.Core.Infra.EFCore/Common/ReadingCommon.cs @@ -1,6 +1,5 @@ using IRaCIS.Core.Domain.Models; using IRaCIS.Core.Domain.Share; -using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; @@ -8,7 +7,6 @@ using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; -using Microsoft.AspNetCore.Hosting; namespace IRaCIS.Core.Infra.EFCore.Common { diff --git a/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs b/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs index a1c877d0..bd4e98c5 100644 --- a/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs +++ b/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs @@ -17,28 +17,31 @@ 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 EntityFramework.Exceptions.Common; +using IRaCIS.Core.Infrastructure; +using System.Data; 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 ) : base(options) { _userInfo = userInfo; - //this._auditingData = auditingData; - //_configuration = configuration; + + _logger = _Logger; + } @@ -476,13 +479,64 @@ namespace IRaCIS.Core.Infra.EFCore AddAudit().GetAwaiter(); 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); + + + var result = 0; + + try + { + + result = 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 查询中存在语法错误。"); + } + + return result; } @@ -651,7 +705,7 @@ namespace IRaCIS.Core.Infra.EFCore #endregion - #endregion + public virtual DbSet TaskAllocationRule { get; set; } diff --git a/IRaCIS.Core.Infra.EFCore/IRaCIS.Core.Infra.EFCore.csproj b/IRaCIS.Core.Infra.EFCore/IRaCIS.Core.Infra.EFCore.csproj index 84f7ff34..5325a1f8 100644 --- a/IRaCIS.Core.Infra.EFCore/IRaCIS.Core.Infra.EFCore.csproj +++ b/IRaCIS.Core.Infra.EFCore/IRaCIS.Core.Infra.EFCore.csproj @@ -28,7 +28,7 @@ - + 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 00000000..cd81f082 --- /dev/null +++ b/IRaCIS.Core.Infrastructure/_IRaCIS/Exception/DBSaveFailedException.cs @@ -0,0 +1,17 @@ +using System; + +namespace IRaCIS.Core.Infrastructure +{ + public class DBSaveFailedException : Exception + { + + public DBSaveFailedException() + { + + } + + public DBSaveFailedException( string message) : base(message) + { + } + } +}