yangzhongke / Zack.EFCore.Batch

Deleting or Updating multiple records from a LINQ Query in a SQL statement without loading entities

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BatchUpdate的时候,全局查询过滤器无法获取到注入的租户信息

FeiCunXinTai opened this issue · comments

ServiceCollection services = new ServiceCollection();

services.AddScoped();
services.AddScoped();
services.AddDbContext(ServiceLifetime.Scoped).AddUnitOfWork();

using (var sp = services.BuildServiceProvider())
{
var session = sp.GetService();
session.IsAuthrizaSuccess = true;
session.UserName = "638051032714457088";
session.CTCloudUserId = "638051032714457088";
session.TenantId = "650058347168673792";
session.ProjectId = 745275920126062592;

using (var uow = sp.GetRequiredService<IUnitOfWork<TestDbContext>>())
{
    using var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);
    var repo = uow.GetRepository<Project_Info>();
    //await repo.BatchDeleteAsync(x => x.Id == 123456);
    await uow.DbContext.BatchUpdate<Project_Info>().Set(q => q.Is_Soft_Deleted, q => false).Where(q => q.Id.Value == 312475030403461).ExecuteAsync();
    scope.Complete();
}

}

大佬,这次准备了一个百度网盘的链接,里面有可测试的控制台,还有建库和测试数据的SQL。修改下连接字符串就可以测试了。
谢谢大佬!

谢谢,我终于能重现这个问题了,之前也有人反馈过。
在QueryFilter中的表达式引用DbContext中的属性就会造成这个问题。这个涉及到EF Core的翻译机制,还没想到思路。最近比较忙,我有时间再去研究怎么解决。
目前只能先通过ExecuteAsync的ignoreQueryFilters参数来让这个组件忽略全局QueryFilter,然后你手动设置过滤条件。

我在这里记个关于自己解决这个问题想法的一个笔记:不再用BatchEFExtensions.Parse的方法来拆解分析SQL语句,而是直接对queryable.ToQueryString()的SQL做语法分析。

SET @__ef_filter__testDateTime_0 = TIMESTAMP '2022-08-09 16:54:53.691911';

SELECT `t`.`Price`, NULL
FROM `T_Books` AS `t`
WHERE ((`t`.`PubTime` > CURRENT_TIMESTAMP()) AND (`t`.`PubTime` > @__ef_filter__testDateTime_0)) AND (`t`.`AuthorName` IS NULL OR (`t`.`AuthorName` = ''))

谢谢大佬,用到租户的表不多,先手动忽略全局QueryFilter
大佬有空再看看这个问题