实体框架核心中的ObjectStateManager.ObjectStateManagerChanged [英] ObjectStateManager.ObjectStateManagerChanged in Entity Framework Core
问题描述
在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)没有公共替代方案。有计划公开终身挂钩,但根据 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.
这篇关于实体框架核心中的ObjectStateManager.ObjectStateManagerChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!