临时修改

IRC_NewDev
hang 2023-10-13 11:03:44 +08:00
parent dfeab8793f
commit 4a2fd0166e
8 changed files with 92 additions and 24 deletions

View File

@ -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;

View File

@ -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<ForeignKey>(sql, paravalue).Select(x => x.Text).ToList());
}
}

View File

@ -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

View File

@ -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);

View File

@ -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
{
/// <summary>
///添加和更新的时候,通常需要与数据库已存在的数据进行校验,添加更新的区分在于是否需要排除自己
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="_dbContext"></param>
/// <param name="isAdd"></param>
/// <param name="verify"></param>
/// <param name="entitydId"></param>
/// <returns></returns>
/// <exception cref="BusinessValidationFailedException"></exception>
public static async Task EntityVerifyAsync<T>(this IRaCISDBContext _dbContext, bool isAdd, EntityVerifyExp<T>[] verify, Guid? entitydId = null) where T : Entity
/// <summary>
///添加和更新的时候,通常需要与数据库已存在的数据进行校验,添加更新的区分在于是否需要排除自己
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="_dbContext"></param>
/// <param name="isAdd"></param>
/// <param name="verify"></param>
/// <param name="entitydId"></param>
/// <returns></returns>
/// <exception cref="BusinessValidationFailedException"></exception>
public static async Task EntityVerifyAsync<T>(this IRaCISDBContext _dbContext, bool isAdd, EntityVerifyExp<T>[] verify, Guid? entitydId = null) where T : Entity
{
if (isAdd)
@ -80,13 +83,13 @@ namespace IRaCIS.Core.Infra.EFCore
}
}
}
}
}
}
public static async Task EntityVerifyAsync<T>(this IRepository<T> _entityRepository, Guid? entitydId = null,params EntityVerifyExp<T>[] verify) where T : Entity
public static async Task EntityVerifyAsync<T>(this IRepository<T> _entityRepository, Guid? entitydId = null, params EntityVerifyExp<T>[] 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的属性条件
/// <summary>EntityState.Detached的实体 修改 部分字段</summary>
public static void EntityModifyPartialFiled<T>(this IRaCISDBContext _dbContext,T waitModifyEntity, Expression<Func<T, T>> updateFactory) where T : Entity
public static void EntityModifyPartialFiled<T>(this IRaCISDBContext _dbContext, T waitModifyEntity, Expression<Func<T, T>> updateFactory) where T : Entity
{
var entityEntry = _dbContext.Entry(waitModifyEntity);
//entityEntry.State = EntityState.Detached;
@ -153,7 +156,7 @@ namespace IRaCIS.Core.Infra.EFCore
#region 不走EF 跟踪机制的删除 更新 以及批量操作
/// <summary>批量删除相当于原生sql 没用EF跟踪方式所有查询出来再删除 浪费性能)</summary>
public static async Task<bool> BatchDeleteNoTrackingAsync<T>(this IRaCISDBContext _dbContext,Expression<Func<T, bool>> deleteFilter) where T : Entity
public static async Task<bool> BatchDeleteNoTrackingAsync<T>(this IRaCISDBContext _dbContext, Expression<Func<T, bool>> deleteFilter) where T : Entity
{
if (deleteFilter == null) throw new ArgumentNullException(nameof(deleteFilter));
@ -162,10 +165,13 @@ namespace IRaCIS.Core.Infra.EFCore
/// <summary>批量更新相当于原生sql 没用EF跟踪方式所有查询出来再更新 浪费性能)</summary>
public static async Task<bool> BatchUpdateNoTrackingAsync<T>(this IRaCISDBContext _dbContext,Expression<Func<T, bool>> where, Expression<Func<T, T>> updateFactory,Guid updateUserId) where T : Entity
public static async Task<bool> BatchUpdateNoTrackingAsync<T>(this IRaCISDBContext _dbContext, Expression<Func<T, bool>> where, Expression<Func<T, T>> 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<Func<T, T>>(member, Expression.Parameter(typeof(T), "x"));
return await _dbContext.Set<T>().IgnoreQueryFilters().Where(where).BatchUpdateAsync(factory).ConfigureAwait(false) > 0;
#endregion
#region efcore 7
//Dictionary<string, object> propValues = new Dictionary<string, object>();
//// 获取表达式体的 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<T>().IgnoreQueryFilters().Where(where).ExecuteUpdateAsync(s => s.SetProperty(t => "UpdateTime", u => "").se).ConfigureAwait(false) > 0;
////return await _dbContext.Set<T>().IgnoreQueryFilters().Where(where).ExecuteUpdateAsync(propValues.SetProperties<T>()).ConfigureAwait(false) > 0;
#endregion
}
#endregion
}

View File

@ -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

View File

@ -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;

View File

@ -11,9 +11,11 @@
<ItemGroup>
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="CSRedisCore" Version="3.8.671" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.12" />
<PackageReference Include="EFCore.BulkExtensions" Version="7.1.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.10" />
<PackageReference Include="EntityFrameworkCore.Exceptions.SqlServer" Version="6.0.3.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.10" />
<PackageReference Include="SharpCompress" Version="0.34.1" />
<PackageReference Include="SharpZipLib" Version="1.4.2" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.3.5" />