MongoDB C# 驱动程序是否支持 Entity Framework 等查询拦截器?
我检查了文档,但找不到任何内容。
基本上,我需要做的是确保对数据库的某些查询(根据上下文)始终应用某些限制。
例如,如果我的文档可以被软删除,那么我总是需要确保为 { "SoftDeleted": false } 添加过滤器。 Entity Framework 通过查询拦截器优雅地处理这个问题。
最佳答案
MongoClient
允许订阅 CommandStartedEvent
。以下是转储到控制台发送到服务器的每个命令的示例:
var mongoClient = new MongoClient(new MongoClientSettings
{
Server = new MongoServerAddress("localhost", 27017),
ClusterConfigurator = cb =>
{
cb.Subscribe<CommandStartedEvent>(e =>
{
Console.WriteLine($"{e.CommandName} - {e.Command.ToJson(new JsonWriterSettings { Indent = true })}");
Console.WriteLine(new String('-', 32));
});
}
});
CommandStartedEvent
包含可用于特定逻辑的 CommandName
和 Command
属性。
关于c# - MongoDB C# 驱动程序 : Query interceptors?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48947260/