实体框架核心2.1,ChangeTracker.Tracked [英] Entity Framework Core 2.1, ChangeTracker.Tracked

查看:118
本文介绍了实体框架核心2.1,ChangeTracker.Tracked的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何使用Entity Framework Cores 2.1新的 ChangeTracker.Tracked事件来吸引阅读查询。不幸的是,我无法理解如何执行此操作。
由于这是一项新功能,因此无法在其上找到任何文章,并且Microsoft官方文档站点未提供任何帮助或示例代码。
我的情况很简单。我有一个包含以下列的数据库:
id,customerId,元数据。
当用户查询该表时,我想截取查询结果集,对于每一行,我都希望将customerId与当前登录的用户进行比较。
我希望 ChangeTracker.Tracked事件可以帮助我拦截返回结果集。我正在寻找有关如何实现上述目标的一些示例代码。

I'm trying to figure out how to use Entity Framework Cores 2.1 new ChangeTracker.Tracked Event to hook into reading queries. Unfortunately, I'm not being able to understand how to implement this. Since it's a new feature it's not possible to find any articles on it and the official Microsoft docs site does not provide any help or sample code. My scenario is pretty simple. I have a database with following columns: id, customerId, metadata. When a user queries this table I want to intercept the query result set and for every row, I want to compare the customerId with currently logged in user. I'm hoping that ChangeTracker.Tracked Event can help me in intercepting the return result set. I'm looking for some sample code on how to achieve above.

推荐答案

以下是 ChangeTracker.Tracked 事件的示例用法。

Here is a sample usage of the ChangeTracker.Tracked event.

将以下方法添加到上下文中(需要使用Microsoft.EntityFrameworkCore.ChangeTracking; ):

Add the following method to your context (requires using Microsoft.EntityFrameworkCore.ChangeTracking;):

void OnEntityTracked(object sender, EntityTrackedEventArgs e)
{
    if (e.FromQuery && e.Entry.Entity is YourEntityClass)
    {
        var entity = (YourEntityClass)e.Entry.Entity;
        bool isCurrentUser = entity.customerId == CurrentUserId;
            // do something (not sure what)
    }
}

并将其附加到 ChangeTracker.Tracked 甚至在您的上下文构造函数中:

and attach it to the ChangeTracker.Tracked even in your context constructor:

ChangeTracker.Tracked += OnEntityTracked;

Tracked 事件文档:


由上下文跟踪实体时触发的事件,可能是因为它是从跟踪查询返回的,还是因为它是附加或添加的

An event fired when an entity is tracked by the context, either because it was returned from a tracking query, or because it was attached or added to the context.

一些要提及的东西。

该事件针对由跟踪查询结果集创建但尚未由跟踪查询结果集创建的每个实体实例触发context

The event is fired for each entity instance created by the tracking query result set and not already tracked by the context

事件args的 bool FromQuery 属性用于区分是否触发了跟踪查询伙伴序列化过程或通过用户代码(附加添加等调用)。

The bool FromQuery property of the event args is used to distinguish if the event is fired from the tracking query materialization process or via user code (Attach, Add etc. calls).

事件args的 EntityEntry条目属性使您可以访问实体实例和其他相关信息(基本上与您在调用非通用 DbContext.Entry 方法)

The EntityEntry Entry property of the event args gives you access to the entity instance and other related information (basically the same information that you get when calling the non-generic DbContext.Entry method)

这篇关于实体框架核心2.1,ChangeTracker.Tracked的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆