批量更新修改

Uat_Study
hang 2022-04-18 17:04:18 +08:00
parent ca933d25a8
commit 7c1fdde2f3
7 changed files with 89 additions and 5 deletions

View File

@ -11,10 +11,12 @@ namespace IRaCIS.Application.Services
public class TestService : BaseService
{
private readonly IRepository<Dictionary> _dicRepository;
private readonly IRepository<Trial> _trialRepository;
public TestService(IRepository<Dictionary> dicRepository)
public TestService(IRepository<Dictionary> dicRepository,IRepository<Trial> 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}", "测试"];

View File

@ -37,6 +37,8 @@ namespace IRaCIS.Core.Infra.EFCore
optionsBuilder.UseLoggerFactory(MyLoggerFactory)
.ReplaceService<IModelCacheKeyFactory, DynamicModelCacheKeyFactoryDesignTimeSupport>();
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<User>().Property(t => t.FullName) .HasDefaultValueSql("[LastName] + ' / ' + [FirstName]");
//modelBuilder.Entity<User>().Property(e => e.FullName).Metadata.SetBeforeSaveBehavior(PropertySaveBehavior.Ignore);

View File

@ -31,6 +31,7 @@ namespace IRaCIS.Core.Infra.EFCore.EntityConfigration
public void Configure(EntityTypeBuilder<Dictionary> builder)
{
builder.Property(e => e.MappedValue).Metadata.SetBeforeSaveBehavior(PropertySaveBehavior.Ignore);
builder.Property(e => e.MappedValue).Metadata.SetAfterSaveBehavior(PropertySaveBehavior.Ignore);
}
}

View File

@ -20,12 +20,14 @@
<PackageReference Include="AutoMapper.Collection" Version="7.0.1" />
<PackageReference Include="AutoMapper.Collection.EntityFrameworkCore" Version="7.1.3" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
<PackageReference Include="EFCore.BulkExtensions" Version="6.4.4" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="6.0.0" />
<PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="6.13.6" />
<PackageReference Include="Zack.EFCore.Batch.MSSQL_NET6" Version="6.0.12" />
</ItemGroup>
</Project>

View File

@ -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<TEntity>()

View File

@ -22,6 +22,12 @@ namespace IRaCIS.Core.Infra.EFCore
Task<TEntity> UpdatePartialFields(Guid id, Expression<Func<TEntity, TEntity>> updateFactory, bool autoSave = false, params EntityVerifyExp<TEntity>[] verify);
Task<bool> BatchUpdateAsync(Expression<Func<TEntity, bool>> where,
Expression<Func<TEntity, TEntity>> updateFactory);
Task<bool> BatchDeleteAsync(Expression<Func<TEntity, bool>> deleteFilter);
}
public interface ICommandRepository<TEntity, TKey> where TEntity : class

View File

@ -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<bool> BatchDeleteAsync(Expression<Func<TEntity, bool>> deleteFilter)
{
return await _dbSet.IgnoreQueryFilters().Where(deleteFilter).BatchDeleteAsync() > 0;
}
public async Task<bool> BatchUpdateAsync(Expression<Func<TEntity, bool>> where,
Expression<Func<TEntity, TEntity>> updateFactory)
{
return await _dbSet.IgnoreQueryFilters().Where(where).BatchUpdateAsync(updateFactory) > 0;
}
/// <summary>
/// 杨中科 test 有问题 对于guid 赋值null的时候
/// </summary>
/// <param name="where"></param>
/// <param name="updateFactory"></param>
/// <returns></returns>
//public async Task<bool> BatchUpdateAsync(Expression<Func<TEntity, bool>> where,
// Expression<Func<TEntity, TEntity>> updateFactory)
//{
// var builder = _dbContext.BatchUpdate<TEntity>();
// Func<TEntity, TEntity> func = updateFactory.Compile();
// List<PropertyInfo> list = ((MemberInitExpression)updateFactory.Body).Bindings.Select<MemberBinding, string>((Func<MemberBinding, string>)(_param1 => _param1.Member.Name)).Select<string, PropertyInfo>((Func<string, PropertyInfo>)(_param1 => (PropertyInfo)typeof(TEntity).GetProperty(_param1, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))).ToList<PropertyInfo>();
// 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<TEntity, c> nameExpr, valueExpr);
// }
// return await builder.Where(where).ExecuteAsync()>0;
//}
#endregion