ObjectStateManager.ObjectStateManagerChanged in the Entity Framework Core

I can not find ObjectStateManager.ObjectStateManagerChangedin ef core. Is it removed, and if so, what is the alternative?

I want to be notified when an object is loaded into context.

+3
source share
1 answer

Currently (the latest official EF Core v1.1.2) there is no public alternative. It is planned to set up Lifetime Hooks , but according to the EF Core Roadmap they are postponed and will not be available in the upcoming version v2.0.

, EF Core /, EF Core

API Core Entity Framework . API .

Microsoft.EntityFrameworkCore.ChangeTracking.Internal , , ILocalViewListener,

void RegisterView(Action<InternalEntityEntry, EntityState> viewAction)

viewAction , , , , , ..

, DbContext ILocalViewListener :

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.

EF Core v1.1.2.

+4

Source: https://habr.com/ru/post/1687970/


All Articles