Entity Framework Core 中的 ObjectStateManager.ObjectStateManagerChanged [英] ObjectStateManager.ObjectStateManagerChanged in Entity Framework Core

查看:17
本文介绍了Entity Framework Core 中的 ObjectStateManager.ObjectStateManagerChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎在 ef 核心中找不到 ObjectStateManager.ObjectStateManagerChanged.这是否已被删除,如果是,有什么替代方法?

I can't seem to find ObjectStateManager.ObjectStateManagerChanged in ef core. Has this been removed and if so what is the alternative?

我想在实体加载到上下文时收到通知.

I want to be notified whenever an entity is loaded into the context.

推荐答案

目前(最新的官方 EF Core v1.1.2)没有公开的替代方案.有计划公开Lifetime Hooks,但根据EF 核心路线图 它们被推迟,在即将发布的 v2.0 版本中将不可用.

Currently (the latest official EF Core v1.1.2) there is no public alternative. There are plans to expose Lifetime Hooks, but according to EF Core Roadmap they are postponed and will not be available in the upcoming v2.0 release.

幸运的是 EF Core 公开了所有内部基础结构类/接口,因此我可以在典型的 EF Core 内部使用免责声明下建议以下解决方法

Luckily EF Core exposes all the internal infrastructure classes/interfaces, so I could suggest the following workaround under the typical EF Core internals usage disclaimer

此 API 支持 Entity Framework Core 基础结构,不应直接从您的代码中使用.此 API 可能会在未来版本中更改或删除.

This API supports the Entity Framework Core infrastructure and is not intended to be used directly from your code. This API may change or be removed in future releases.

我想到的是利用 Microsoft.EntityFrameworkCore.ChangeTracking.Internal 命名空间,更具体地说是提供以下方法的 ILocalViewListener 接口

What I have in mind is utilizing the Microsoft.EntityFrameworkCore.ChangeTracking.Internal namespace, and more specifically the ILocalViewListener interface which provides the following method

void RegisterView(Action<InternalEntityEntry, EntityState> viewAction)

viewAction 允许您注册将在任何实体状态更改时调用的委托,包括附加、添加、删除、分离等.

The viewAction allows you to register delegate which will be called on any entity state change, including attaching, adding, deleting, detaching etc.

因此在您的 DbContext 派生类构造函数中,您可以获得 ILocalViewListener 服务并像这样注册处理程序:

So inside your DbContext derived class constructor, you could obtain ILocalViewListener service and register the handler like this:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;

public class MyDbContext : DbContext
{
    // ...
    public MyDbContext()
    {
        this.GetService<ILocalViewListener>()?.RegisterView(OnStateManagerChanged);
    }

    void OnStateManagerChanged(InternalEntityEntry entry, EntityState previousState)
    {
        if (previousState == EntityState.Detached && entry.EntityState == EntityState.Unchanged)
        {
            // Process loaded entity
            var entity = entry.Entity;
        }
    }
}

在处理程序内部,您还可以使用 InternalEntityEntry.ToEntityEntry() 方法来获取常规的 EntityEntry 对象(类似于 DbContext.Entry> 和 ChangeTracker.Entries 方法),如果您更喜欢使用它而不是内部 InternalEntityEntry 类.

Inside the handler, you can also use the InternalEntityEntry.ToEntityEntry() method to get the regular EntityEntry object (similar to DbContext.Entry and ChangeTracker.Entries methods) if you prefer working with it rather than the internal InternalEntityEntry class.

在 EF Core v1.1.2 中测试和工作.

Tested and working in EF Core v1.1.2.

这篇关于Entity Framework Core 中的 ObjectStateManager.ObjectStateManagerChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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