ef core操作达梦数据库,提示 【查询使用值表达式作为过滤条件】
这是因为ef core转成达梦sql的时候,当条件不匹配时,打印sql,发现会转成where 0 而不是 where 0=1;因此,可以使用Ef core的拦截器,进行修改,把where 0转为 where 0=1;启动运行,成功将where 0改为 where 0=1;同步异步尽量都加上,不然可能会漏掉拦截。也可以使用官网的方式进行添加。
·
这是因为ef core转成达梦sql的时候,当条件不匹配时,打印sql,发现会转成where 0 而不是 where 0=1;因此,可以使用Ef core的拦截器,进行修改,把where 0转为 where 0=1;
Ef core拦截器官网地址
首先,继承拦截器,代码如下:
public class DMInterceptor : DbCommandInterceptor
{
public override InterceptionResult<DbDataReader> ReaderExecuting(
DbCommand command,
CommandEventData eventData,
InterceptionResult<DbDataReader> result)
{
ManipulateCommand(command);
//return base.ReaderExecuting(command, eventData, result);
return result;
}
public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(
DbCommand command,
CommandEventData eventData,
InterceptionResult<DbDataReader> result,
CancellationToken cancellationToken = default)
{
ManipulateCommand(command);
return new ValueTask<InterceptionResult<DbDataReader>>(result);
}
/// <summary>
/// 达梦特殊查询转化
/// </summary>
/// <param name="command"></param>
private static void ManipulateCommand(DbCommand command)
{
// 这里可以修改 SQL 查询,添加自定义的逻辑
if (command.CommandText.EndsWith("WHERE 0"))
{
command.CommandText = command.CommandText.Replace("WHERE 0", "WHERE 0=1");
}
}
}
同步异步尽量都加上,不然可能会漏掉拦截。
然后使用AddInterceptors进行添加:
builder.UseDm(connectionString).AddInterceptors(new DMInterceptor ());
也可以使用官网的方式进行添加。 如下
private static readonly DMInterceptor _interceptor
= new DMInterceptor ();
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.AddInterceptors(_interceptor);
启动运行,成功将where 0改为 where 0=1;
ManipulateCommand方法根据具体情况进行调整

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐
所有评论(0)