From 4a2fd0166e1f57662a385b177de0fb5034d6c700 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Fri, 13 Oct 2023 11:03:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=B4=E6=97=B6=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BackGroundJob/IRaCISCHangfireJob.cs | 1 - .../Inspection/FrontAuditConfigService.cs | 2 + .../Allocation/TaskConsistentRule.cs | 1 - IRaCIS.Core.Infra.EFCore/Common/EFSqlQuery.cs | 2 +- .../Repository/IRaCISContextExtension.cs | 104 +++++++++++++++--- .../Repository/IRepository.cs | 1 - .../Repository/Repository.cs | 1 - .../IRaCIS.Core.Infrastructure.csproj | 4 +- 8 files changed, 92 insertions(+), 24 deletions(-) diff --git a/IRaCIS.Core.Application/BackGroundJob/IRaCISCHangfireJob.cs b/IRaCIS.Core.Application/BackGroundJob/IRaCISCHangfireJob.cs index f62423549..b5bcb0565 100644 --- a/IRaCIS.Core.Application/BackGroundJob/IRaCISCHangfireJob.cs +++ b/IRaCIS.Core.Application/BackGroundJob/IRaCISCHangfireJob.cs @@ -5,7 +5,6 @@ using IRaCIS.Core.Application.Helper; using IRaCIS.Core.Application.Service; using IRaCIS.Core.Domain.Share; using IRaCIS.Core.Infrastructure; -using Microsoft.EntityFrameworkCore.SqlServer.Query.Internal; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Newtonsoft.Json; diff --git a/IRaCIS.Core.Application/Service/Inspection/FrontAuditConfigService.cs b/IRaCIS.Core.Application/Service/Inspection/FrontAuditConfigService.cs index dd130c66b..559fff3a5 100644 --- a/IRaCIS.Core.Application/Service/Inspection/FrontAuditConfigService.cs +++ b/IRaCIS.Core.Application/Service/Inspection/FrontAuditConfigService.cs @@ -571,6 +571,8 @@ namespace IRaCIS.Core.Application.Service SqlParameter[] paravalue = new SqlParameter[] { new SqlParameter("@para",para) }; + + //https://learn.microsoft.com/zh-tw/ef/core/querying/sql-queries jsonDataValueDic[item.Key] = string.Join(",", _frontAuditConfigRepository._dbContext.Database.SqlQuery(sql, paravalue).Select(x => x.Text).ToList()); } } diff --git a/IRaCIS.Core.Domain/Allocation/TaskConsistentRule.cs b/IRaCIS.Core.Domain/Allocation/TaskConsistentRule.cs index 1ce5902dc..05a06767c 100644 --- a/IRaCIS.Core.Domain/Allocation/TaskConsistentRule.cs +++ b/IRaCIS.Core.Domain/Allocation/TaskConsistentRule.cs @@ -6,7 +6,6 @@ using System; using System.ComponentModel.DataAnnotations.Schema; using System.Collections.Generic; -using EntityFrameworkCore.Projectables; using System.Linq; namespace IRaCIS.Core.Domain.Models diff --git a/IRaCIS.Core.Infra.EFCore/Common/EFSqlQuery.cs b/IRaCIS.Core.Infra.EFCore/Common/EFSqlQuery.cs index 38c84a998..d566f122c 100644 --- a/IRaCIS.Core.Infra.EFCore/Common/EFSqlQuery.cs +++ b/IRaCIS.Core.Infra.EFCore/Common/EFSqlQuery.cs @@ -56,7 +56,7 @@ namespace IRaCIS.Core.Infra.EFCore dbConn = conn; conn.Open(); DbCommand cmd = conn.CreateCommand(); - if (facade.IsSqlServer()) + if (facade.IsRelational()) { cmd.CommandText = sql; CombineParams(ref cmd, parameters); diff --git a/IRaCIS.Core.Infra.EFCore/Repository/IRaCISContextExtension.cs b/IRaCIS.Core.Infra.EFCore/Repository/IRaCISContextExtension.cs index 547a5119b..0687678aa 100644 --- a/IRaCIS.Core.Infra.EFCore/Repository/IRaCISContextExtension.cs +++ b/IRaCIS.Core.Infra.EFCore/Repository/IRaCISContextExtension.cs @@ -1,9 +1,12 @@ -using EFCore.BulkExtensions; +using AutoMapper.Internal; +using EFCore.BulkExtensions; using IRaCIS.Core.Domain.Models; using IRaCIS.Core.Infrastructure; using IRaCIS.Core.Infrastructure.Extention; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Query; using System; +using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; @@ -38,17 +41,17 @@ namespace IRaCIS.Core.Infra.EFCore public static class EntityAction { - /// - ///添加和更新的时候,通常需要与数据库已存在的数据进行校验,添加更新的区分在于是否需要排除自己 - /// - /// - /// - /// - /// - /// - /// - /// - public static async Task EntityVerifyAsync(this IRaCISDBContext _dbContext, bool isAdd, EntityVerifyExp[] verify, Guid? entitydId = null) where T : Entity + /// + ///添加和更新的时候,通常需要与数据库已存在的数据进行校验,添加更新的区分在于是否需要排除自己 + /// + /// + /// + /// + /// + /// + /// + /// + public static async Task EntityVerifyAsync(this IRaCISDBContext _dbContext, bool isAdd, EntityVerifyExp[] verify, Guid? entitydId = null) where T : Entity { if (isAdd) @@ -80,13 +83,13 @@ namespace IRaCIS.Core.Infra.EFCore } } } - } + } } - public static async Task EntityVerifyAsync(this IRepository _entityRepository, Guid? entitydId = null,params EntityVerifyExp[] verify) where T : Entity + public static async Task EntityVerifyAsync(this IRepository _entityRepository, Guid? entitydId = null, params EntityVerifyExp[] verify) where T : Entity { - if (entitydId==null) + if (entitydId == null) { foreach (var verifyItem in verify.Where(t => t.verifyType != VerifyEnum.OnlyUpdate && t.IsVerify)) { @@ -122,7 +125,7 @@ namespace IRaCIS.Core.Infra.EFCore ///注意 模型标注了 ConcurrencyCheck的属性,这样的实体,不适合用部分字段更新,ef生成的更新sql会自动带上ConcurrencyCheck的属性条件 /// EntityState.Detached的实体 修改 部分字段 - public static void EntityModifyPartialFiled(this IRaCISDBContext _dbContext,T waitModifyEntity, Expression> updateFactory) where T : Entity + public static void EntityModifyPartialFiled(this IRaCISDBContext _dbContext, T waitModifyEntity, Expression> updateFactory) where T : Entity { var entityEntry = _dbContext.Entry(waitModifyEntity); //entityEntry.State = EntityState.Detached; @@ -153,7 +156,7 @@ namespace IRaCIS.Core.Infra.EFCore #region 不走EF 跟踪机制的删除 更新 以及批量操作 /// 批量删除,相当于原生sql, 没用EF跟踪方式(所有查询出来,再删除 浪费性能) - public static async Task BatchDeleteNoTrackingAsync(this IRaCISDBContext _dbContext,Expression> deleteFilter) where T : Entity + public static async Task BatchDeleteNoTrackingAsync(this IRaCISDBContext _dbContext, Expression> deleteFilter) where T : Entity { if (deleteFilter == null) throw new ArgumentNullException(nameof(deleteFilter)); @@ -162,10 +165,13 @@ namespace IRaCIS.Core.Infra.EFCore /// 批量更新,相当于原生sql, 没用EF跟踪方式(所有查询出来,再更新 浪费性能) - public static async Task BatchUpdateNoTrackingAsync(this IRaCISDBContext _dbContext,Expression> where, Expression> updateFactory,Guid updateUserId) where T : Entity + public static async Task BatchUpdateNoTrackingAsync(this IRaCISDBContext _dbContext, Expression> where, Expression> updateFactory, Guid updateUserId) where T : Entity { if (where == null) throw new ArgumentNullException(nameof(where)); + + #region batch delete bug + var bindings = ((MemberInitExpression)updateFactory.Body).Bindings.ToList(); var hasPropNameList = bindings.Select(t => t.Member.Name).ToList(); @@ -193,12 +199,74 @@ namespace IRaCIS.Core.Infra.EFCore var factory = Expression.Lambda>(member, Expression.Parameter(typeof(T), "x")); + return await _dbContext.Set().IgnoreQueryFilters().Where(where).BatchUpdateAsync(factory).ConfigureAwait(false) > 0; + #endregion + + + #region efcore 7 + + //Dictionary propValues = new Dictionary(); + + //// 获取表达式体的 MemberInitExpression + //var memberInitExpr = (MemberInitExpression)updateFactory.Body; + + + + //// 获取初始化的绑定信息 + //var bindings = memberInitExpr.Bindings.ToList(); + + //var hasPropNameList = bindings.Select(t => t.Member.Name).ToList(); + + + //// 遍历绑定信息 + //foreach (MemberAssignment binding in bindings) + //{ + // // 获取属性名 + // string propName = binding.Member.Name; + + // // 获取属性值,需要对表达式进行编译和调用 + // var propValue = Expression.Lambda(binding.Expression).Compile().DynamicInvoke(); + + // // 现在,你可以访问属性名和属性值 + // propValues.Add(propName, propValue); + //} + + //if (typeof(IAuditUpdate).IsAssignableFrom(typeof(T))) + //{ + + // if (!hasPropNameList.Contains(nameof(IAuditUpdate.UpdateTime))) + // { + // propValues.Add(nameof(IAuditUpdate.UpdateTime), DateTime.Now); + + + // } + + // if (!hasPropNameList.Contains(nameof(IAuditUpdate.UpdateUserId))) + // { + // propValues.Add(nameof(IAuditUpdate.UpdateUserId), updateUserId); + + // } + + //} + + //return await _dbContext.Set().IgnoreQueryFilters().Where(where).ExecuteUpdateAsync(s => s.SetProperty(t => "UpdateTime", u => "").se).ConfigureAwait(false) > 0; + + ////return await _dbContext.Set().IgnoreQueryFilters().Where(where).ExecuteUpdateAsync(propValues.SetProperties()).ConfigureAwait(false) > 0; + + #endregion + + + + } + + + #endregion } diff --git a/IRaCIS.Core.Infra.EFCore/Repository/IRepository.cs b/IRaCIS.Core.Infra.EFCore/Repository/IRepository.cs index 698d295bb..616d242ca 100644 --- a/IRaCIS.Core.Infra.EFCore/Repository/IRepository.cs +++ b/IRaCIS.Core.Infra.EFCore/Repository/IRepository.cs @@ -12,7 +12,6 @@ using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; using IRaCIS.Core.Domain.Share; -using EFCore.BulkExtensions; using Microsoft.Extensions.Localization; namespace IRaCIS.Core.Infra.EFCore diff --git a/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs b/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs index b445532c2..7901a0a84 100644 --- a/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs +++ b/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs @@ -10,7 +10,6 @@ using IRaCIS.Core.Domain.Models; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using AutoMapper.QueryableExtensions; -using EFCore.BulkExtensions; using IRaCIS.Core.Domain.Share; using IRaCIS.Core.Infrastructure; using IRaCIS.Core.Infrastructure.Extention; diff --git a/IRaCIS.Core.Infrastructure/IRaCIS.Core.Infrastructure.csproj b/IRaCIS.Core.Infrastructure/IRaCIS.Core.Infrastructure.csproj index 2241479bb..16fa0af42 100644 --- a/IRaCIS.Core.Infrastructure/IRaCIS.Core.Infrastructure.csproj +++ b/IRaCIS.Core.Infrastructure/IRaCIS.Core.Infrastructure.csproj @@ -11,9 +11,11 @@ - + + +