From 7c1fdde2f339727460eabaca2e583d9d47d8b193 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Mon, 18 Apr 2022 17:04:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=B9=E9=87=8F=E6=9B=B4=E6=96=B0=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IRaCIS.Core.Application/TestService.cs | 21 +++++-- .../Context/IRaCISDBContext.cs | 3 +- .../EntityConfigration/DoctorConfigration.cs | 1 + .../IRaCIS.Core.Infra.EFCore.csproj | 2 + .../Interceptor/SoftDeleteQueryExtension.cs | 1 + .../Repository/ICommandRepository.cs | 6 ++ .../Repository/Repository.cs | 60 +++++++++++++++++++ 7 files changed, 89 insertions(+), 5 deletions(-) diff --git a/IRaCIS.Core.Application/TestService.cs b/IRaCIS.Core.Application/TestService.cs index c60c6adc2..ee20dd47d 100644 --- a/IRaCIS.Core.Application/TestService.cs +++ b/IRaCIS.Core.Application/TestService.cs @@ -11,10 +11,12 @@ namespace IRaCIS.Application.Services public class TestService : BaseService { private readonly IRepository _dicRepository; + private readonly IRepository _trialRepository; - public TestService(IRepository dicRepository) + public TestService(IRepository dicRepository,IRepository trialRepository) { _dicRepository = dicRepository; + _trialRepository = trialRepository; } [HttpPost] @@ -25,11 +27,22 @@ namespace IRaCIS.Application.Services var c = _dicRepository.Where(t => t.ParentId != null).Select(t => t.MappedValue).First(); CultureInfo culture = CultureInfo.CurrentUICulture; - var dd= _dicRepository.UpdatePartialFields(Guid.Parse("8a90c96e-0776-4f7b-82a6-18933d339584"), - u => new Dictionary() { ParentId = null, Code = "test" }, true).Result; + //var dd= _dicRepository.UpdatePartialFields(Guid.Parse("8a90c96e-0776-4f7b-82a6-18933d339584"), + // u => new Dictionary() { ParentId = null, Code = "test" }, true).Result; + + var aaaa = _dicRepository.BatchDeleteAsync(t => t.Id == Guid.Empty).Result; + + //var ggggg = _dicRepository.UpdateFromQueryAsync(t => t.ParentId == Guid.Parse("8a90c96e-0776-4f7b-82a6-18933d339584"), u=>new Dictionary(){Code = "test"}).Result; + + var ddd = _dicRepository.BatchUpdateAsync(t => t.Id == Guid.Parse("8a90c96e-0776-4f7b-82a6-18933d339584"), + u => new Dictionary() { ParentId = null, Code = "test" }).Result; - var d = _dicRepository.UpdateFromDTOAsync(new AddOrEditBasicDic() { Id = Guid.Parse("60d86683-c33b-4349-b672-08da1e91b622"), ParentId = null, ChildGroup = null, Code = null }, true, true).Result; + var aaaaa= _trialRepository.BatchUpdateAsync(t => t.Id == Guid.Empty, + u => new Trial { TrialCode = "test" }).Result; + + + //var d = _dicRepository.UpdateFromDTOAsync(new AddOrEditBasicDic() { Id = Guid.Parse("60d86683-c33b-4349-b672-08da1e91b622"), ParentId = null, ChildGroup = null, Code = null }, true, true).Result; var a = 123; var b = _localizer["test{0}", "测试"]; diff --git a/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs b/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs index 129460030..b1781c0f5 100644 --- a/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs +++ b/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs @@ -37,6 +37,8 @@ namespace IRaCIS.Core.Infra.EFCore optionsBuilder.UseLoggerFactory(MyLoggerFactory) .ReplaceService(); + optionsBuilder.UseBatchEF_MSSQL(); + //var config = new ConfigurationBuilder() // .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true).Build(); //connectionString = config.GetSection("ConnectionStrings:RemoteNew").Value; @@ -74,7 +76,6 @@ namespace IRaCIS.Core.Infra.EFCore } - //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/EntityConfigration/DoctorConfigration.cs b/IRaCIS.Core.Infra.EFCore/EntityConfigration/DoctorConfigration.cs index ddef1e982..afcafa48f 100644 --- a/IRaCIS.Core.Infra.EFCore/EntityConfigration/DoctorConfigration.cs +++ b/IRaCIS.Core.Infra.EFCore/EntityConfigration/DoctorConfigration.cs @@ -31,6 +31,7 @@ namespace IRaCIS.Core.Infra.EFCore.EntityConfigration public void Configure(EntityTypeBuilder builder) { builder.Property(e => e.MappedValue).Metadata.SetBeforeSaveBehavior(PropertySaveBehavior.Ignore); + builder.Property(e => e.MappedValue).Metadata.SetAfterSaveBehavior(PropertySaveBehavior.Ignore); } } diff --git a/IRaCIS.Core.Infra.EFCore/IRaCIS.Core.Infra.EFCore.csproj b/IRaCIS.Core.Infra.EFCore/IRaCIS.Core.Infra.EFCore.csproj index 63033e2bc..fa3b4d973 100644 --- a/IRaCIS.Core.Infra.EFCore/IRaCIS.Core.Infra.EFCore.csproj +++ b/IRaCIS.Core.Infra.EFCore/IRaCIS.Core.Infra.EFCore.csproj @@ -20,12 +20,14 @@ + + diff --git a/IRaCIS.Core.Infra.EFCore/Interceptor/SoftDeleteQueryExtension.cs b/IRaCIS.Core.Infra.EFCore/Interceptor/SoftDeleteQueryExtension.cs index 3d26e333a..5f517893f 100644 --- a/IRaCIS.Core.Infra.EFCore/Interceptor/SoftDeleteQueryExtension.cs +++ b/IRaCIS.Core.Infra.EFCore/Interceptor/SoftDeleteQueryExtension.cs @@ -21,6 +21,7 @@ namespace IRaCIS.Core.Infra.EFCore .MakeGenericMethod(entityData.ClrType); var filter = methodToCall.Invoke(null, new object[] { }); entityData.SetQueryFilter((LambdaExpression)filter); + } private static LambdaExpression GetSoftDeleteFilter() diff --git a/IRaCIS.Core.Infra.EFCore/Repository/ICommandRepository.cs b/IRaCIS.Core.Infra.EFCore/Repository/ICommandRepository.cs index ea895984a..1d09171d0 100644 --- a/IRaCIS.Core.Infra.EFCore/Repository/ICommandRepository.cs +++ b/IRaCIS.Core.Infra.EFCore/Repository/ICommandRepository.cs @@ -22,6 +22,12 @@ namespace IRaCIS.Core.Infra.EFCore Task UpdatePartialFields(Guid id, Expression> updateFactory, bool autoSave = false, params EntityVerifyExp[] verify); + + Task BatchUpdateAsync(Expression> where, + Expression> updateFactory); + + Task BatchDeleteAsync(Expression> deleteFilter); + } 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 3adf611a2..f53add7aa 100644 --- a/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs +++ b/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs @@ -12,6 +12,7 @@ using IRaCIS.Core.Domain.Models; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using AutoMapper.QueryableExtensions; +using EFCore.BulkExtensions; using IRaCIS.Core.Infrastructure; using IRaCIS.Core.Infrastructure.Extention; using Microsoft.Data.SqlClient; @@ -511,6 +512,65 @@ namespace IRaCIS.Core.Infra.EFCore return await _dbSet.IgnoreQueryFilters().Where(where).UpdateFromQueryAsync(updateFactory) > 0; } + + public async Task BatchDeleteAsync(Expression> deleteFilter) + { + return await _dbSet.IgnoreQueryFilters().Where(deleteFilter).BatchDeleteAsync() > 0; + } + + + public async Task BatchUpdateAsync(Expression> where, + Expression> updateFactory) + { + + return await _dbSet.IgnoreQueryFilters().Where(where).BatchUpdateAsync(updateFactory) > 0; + } + + + /// + /// 杨中科 test 有问题 对于guid? 赋值null的时候 + /// + /// + /// + /// + //public async Task BatchUpdateAsync(Expression> where, + // Expression> updateFactory) + //{ + // var builder = _dbContext.BatchUpdate(); + + // Func func = updateFactory.Compile(); + + // List list = ((MemberInitExpression)updateFactory.Body).Bindings.Select((Func)(_param1 => _param1.Member.Name)).Select((Func)(_param1 => (PropertyInfo)typeof(TEntity).GetProperty(_param1, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))).ToList(); + + // TEntity applyObj = func(new TEntity()); + + // foreach (PropertyInfo prop in list) + // { + // var propName = prop.Name; + // var value = prop.GetValue(applyObj); + + + // builder.Set(propName, value?? DBNull.Value); + + // //PropertyInfo property = typeof(TEntity).GetProperty(prop.Name); + // //Type propertyType = property.PropertyType; + // //ParameterExpression parameterExpression = Expression.Parameter(typeof(TEntity)); + // //Type delegateType = typeof(Func<,>).MakeGenericType(typeof(TEntity), propertyType); + // //LambdaExpression nameExpr = Expression.Lambda(delegateType, (Expression)Expression.MakeMemberAccess((Expression)parameterExpression, (MemberInfo)property), parameterExpression); + // //Expression expression = (Expression)Expression.Constant(value); + + // //expression = (Expression)Expression.Convert(expression, propertyType); + + // //LambdaExpression valueExpr = Expression.Lambda(delegateType, expression, parameterExpression); + + + // //builder.Set< >(Func nameExpr, valueExpr); + + // } + // return await builder.Where(where).ExecuteAsync()>0; + //} + + #endregion