首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在EF core 3中拦截数据库操作?

如何在EF core 3中拦截数据库操作?
EN

Stack Overflow用户
提问于 2019-10-11 05:25:26
回答 1查看 2.1K关注 0票数 6

在PostgreSQL中使用EF core3进行选择/删除/插入/更新等数据库操作之前如何拦截?

在EF Core3.0 https://docs.microsoft.com/en-gb/ef/core/what-is-new/ef-core-3.0/#interception-of-database-operations的新特性中,使用了DbCommandInterceptor。但是,我找不到这个类,也找不到AddInterceptors

我是否需要安装新的nuget或使用特定的命名空间?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-11 06:30:27

我已经找到了我需要的东西,并成功地实现了。

代码语言:javascript
复制
using System.Data.Common;
using Microsoft.EntityFrameworkCore.Diagnostics;

public class RowLevelSecurityInterceptor : DbCommandInterceptor
{
  public override InterceptionResult<DbDataReader> ReaderExecuting(
    DbCommand command,
    CommandEventData eventData,
    InterceptionResult<DbDataReader> result)
  {
    // command.CommandText += " OPTION (OPTIMIZE FOR UNKNOWN)";
    return result;
  }
}
代码语言:javascript
复制
public class MyDbContext : DbContext
{
  // previous codes
  protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  {

    optionsBuilder
      .UseNpgsql(GetConnectionString())
      .AddInterceptors(new RowLevelSecurityInterceptor());
    }
  }
}
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58331311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档