From 4cc64e1e21735486b80efef256134313e79fa721 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Fri, 15 Apr 2022 10:11:41 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=20=E4=BB=93=E5=82=A8?= =?UTF-8?q?=EF=BC=8C=E6=8F=90=E5=87=BA=E7=8B=AC=E7=AB=8B=E7=9A=84=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=92=8C=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Service/Common/CommonDocumentService.cs | 2 +- IRaCIS.Core.Domain/Management/User.cs | 2 +- .../Context/IRaCISDBContext.cs | 11 +- .../Repository/ICommandRepository.cs | 10 +- .../Repository/Repository.cs | 111 +++++++++++------- 5 files changed, 79 insertions(+), 57 deletions(-) diff --git a/IRaCIS.Core.Application/Service/Common/CommonDocumentService.cs b/IRaCIS.Core.Application/Service/Common/CommonDocumentService.cs index a0bb8e1c9..057d1dbd2 100644 --- a/IRaCIS.Core.Application/Service/Common/CommonDocumentService.cs +++ b/IRaCIS.Core.Application/Service/Common/CommonDocumentService.cs @@ -47,7 +47,7 @@ namespace IRaCIS.Core.Application.Service VerifyMsg = "Document Code Can not Repeat." }; - var entity = await _commonDocumentRepository.InsertOrUpdateAsync(addOrEditCommonDocument, true, verifyExp1); + var entity = await _commonDocumentRepository.InsertFromDTOAsync(addOrEditCommonDocument, true, verifyExp1); return ResponseOutput.Ok(entity.Id.ToString()); diff --git a/IRaCIS.Core.Domain/Management/User.cs b/IRaCIS.Core.Domain/Management/User.cs index 41fb43432..d34724f48 100644 --- a/IRaCIS.Core.Domain/Management/User.cs +++ b/IRaCIS.Core.Domain/Management/User.cs @@ -68,7 +68,7 @@ namespace IRaCIS.Core.Domain.Models public bool IsFirstAdd { get; set; } = true; - [NotMapped] + [NotMapped] public string FullName { get; set; } } } diff --git a/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs b/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs index 75eba7b84..b01a0a09e 100644 --- a/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs +++ b/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs @@ -45,13 +45,6 @@ namespace IRaCIS.Core.Infra.EFCore #region IModelCacheKeyFactory - //public class DynamicModelCacheKeyFactory : IModelCacheKeyFactory - //{ - // public object Create(DbContext context) - // => context is IRaCISDBContext dynamicContext - // ? (context.GetType(), dynamicContext._userInfo.IsEn_Us) - // : (object)context.GetType(); - //} public class DynamicModelCacheKeyFactoryDesignTimeSupport : IModelCacheKeyFactory { @@ -82,8 +75,8 @@ namespace IRaCIS.Core.Infra.EFCore - //modelBuilder.Entity().Property(t => t.FullName) .HasComputedColumnSql("[LastName] + ' / ' + [FirstName]",false); - //modelBuilder.Entity().Ignore(t => t.FullName); + modelBuilder.Entity().Property(t => t.FullName) .HasDefaultValueSql("[LastName] + ' / ' + [FirstName]"); + modelBuilder.Entity().Property(e => e.FullName).Metadata.SetBeforeSaveBehavior(PropertySaveBehavior.Ignore); //遍历实体模型手动配置 diff --git a/IRaCIS.Core.Infra.EFCore/Repository/ICommandRepository.cs b/IRaCIS.Core.Infra.EFCore/Repository/ICommandRepository.cs index 6268b9dca..6066bfd01 100644 --- a/IRaCIS.Core.Infra.EFCore/Repository/ICommandRepository.cs +++ b/IRaCIS.Core.Infra.EFCore/Repository/ICommandRepository.cs @@ -11,9 +11,15 @@ namespace IRaCIS.Core.Infra.EFCore { public interface ICommandRepository: ICommandRepository where TEntity : Entity { - Task InsertDictionaryAsync(TFrom from, params EntityVerifyExp[] verify); + //Task InsertDictionaryAsync(TFrom from, params EntityVerifyExp[] verify); Task InsertOrUpdateAsync(TFrom from, bool autoSave = false, params EntityVerifyExp[] verify); - + + + Task InsertFromDTOAsync(TFrom from, bool autoSave = false, params EntityVerifyExp[] verify); + + + Task UpdateFromDTOAsync(TFrom from, bool autoSave = false, params EntityVerifyExp[] verify); + } public interface ICommandRepository where TEntity : class diff --git a/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs b/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs index 43ee744dd..1c35e4acc 100644 --- a/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs +++ b/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs @@ -65,8 +65,8 @@ namespace IRaCIS.Core.Infra.EFCore Type type = typeof(TFrom); //以下是不要ID这个字段的 比如自增列ID 就不能像上名那样写 - var properties = type.GetProperties().Where(t=>t.Name!="Id"); - + var properties = type.GetProperties().Where(t => t.Name != "Id"); + string strSqlName = string.Join(",", properties.Select(p => $"[{p.Name}]").ToArray()); @@ -97,63 +97,84 @@ namespace IRaCIS.Core.Infra.EFCore if (entity.Id == Guid.Empty) { + return await InsertFromDTOAsync(from, autoSave, verify); + } + else + { + return await UpdateFromDTOAsync(from, autoSave, verify); + } + } - foreach (var verifyItem in verify.Where(t => t.verifyType != VerifyEnum.OnlyUpdate && t.IsVerify)) + public async Task InsertFromDTOAsync(TFrom from, bool autoSave = false, params EntityVerifyExp[] verify) + { + var entity = _mapper.Map(from); + + foreach (var verifyItem in verify.Where(t => t.verifyType != VerifyEnum.OnlyUpdate && t.IsVerify)) + { + if (await _dbSet.IgnoreQueryFilters().AnyAsync(verifyItem.VerifyExp).ConfigureAwait(false)) + { + throw new BusinessValidationFailedException(verifyItem.VerifyMsg); + } + } + + await _dbSet.AddAsync(entity).ConfigureAwait(false); + + if (autoSave) + { + await SaveChangesAsync(); + + } + return entity; + + } + + + public async Task UpdateFromDTOAsync(TFrom from, bool autoSave = false, + params EntityVerifyExp[] verify) + { + + var entity = _mapper.Map(from); + + foreach (var verifyItem in verify.Where(t => t.verifyType != VerifyEnum.OnlyAdd && t.IsVerify)) + { + if (verifyItem.verifyType == VerifyEnum.OnlyUpdate) { if (await _dbSet.IgnoreQueryFilters().AnyAsync(verifyItem.VerifyExp).ConfigureAwait(false)) { throw new BusinessValidationFailedException(verifyItem.VerifyMsg); } } - - await _dbSet.AddAsync(entity).ConfigureAwait(false); - - if (autoSave) + else if (verifyItem.verifyType == VerifyEnum.Both) { - await SaveChangesAsync(); - + if (await _dbSet.IgnoreQueryFilters().AnyAsync(verifyItem.VerifyExp.And(t => t.Id != entity.Id)).ConfigureAwait(false)) + { + throw new BusinessValidationFailedException(verifyItem.VerifyMsg); + } } - return entity; - } - else + + var dbEntity = await _dbSet.IgnoreQueryFilters().FirstOrDefaultAsync(t => t.Id == entity.Id).ConfigureAwait(false); + + if (dbEntity == null) { - - - foreach (var verifyItem in verify.Where(t => t.verifyType != VerifyEnum.OnlyAdd && t.IsVerify)) - { - if (verifyItem.verifyType == VerifyEnum.OnlyUpdate) - { - if (await _dbSet.IgnoreQueryFilters().AnyAsync(verifyItem.VerifyExp).ConfigureAwait(false)) - { - throw new BusinessValidationFailedException(verifyItem.VerifyMsg); - } - } - else if (verifyItem.verifyType == VerifyEnum.Both) - { - if (await _dbSet.IgnoreQueryFilters().AnyAsync(verifyItem.VerifyExp.And(t => t.Id != entity.Id)).ConfigureAwait(false)) - { - throw new BusinessValidationFailedException(verifyItem.VerifyMsg); - } - } - } - - var dbEntity = await _dbSet.IgnoreQueryFilters().FirstOrDefaultAsync(t => t.Id == entity.Id).ConfigureAwait(false); - - var dbBeforEntity = dbEntity.Clone(); - - _mapper.Map(from, dbEntity); - - if (autoSave) - { - await SaveChangesAsync(); - } - return dbBeforEntity; - + throw new BusinessValidationFailedException( + " Update object not exist in db,Please check if the parameter Id is passed incorrectly"); } + + var dbBeforEntity = dbEntity.Clone(); + + _mapper.Map(from, dbEntity); + + if (autoSave) + { + await SaveChangesAsync(); + } + return dbBeforEntity; } + + public async Task SaveChangesAsync(CancellationToken cancellationToken = default) { @@ -198,6 +219,8 @@ namespace IRaCIS.Core.Infra.EFCore return entity; } + + public async Task> AddRangeAsync(IEnumerable entities) { await _dbSet.AddRangeAsync(entities).ConfigureAwait(false);