SaveChanges 后无法获取更改的实体

问题描述 投票:0回答:2

数据成功更改后我需要做一些日志记录。我正在使用实体框架核心来实现此目的。在将更改保存到数据库之前调用

SavingChanges
事件处理程序,在保存更改之后调用
SavedChanges
事件处理程序。在
SavingChanges
中,我可以使用
context.ChangeTracker.Entries()
获取修改后的实体,并选择状态已更改的实体
context.ChangeTracker.Entries().Where(e => e.State != EntityState.Unchanged)
,但在
SavedChanges
事件中,即使我有 3 个已更改的实体,所有条目都显示不变。有人可以建议解决我的问题的好方法吗?

c# .net .net-core entity-framework-core
2个回答
2
投票

一旦它们被保存,它们现在就是

unchanged
,因为它们代表了这些实体的数据库的当前状态。记录在
SavingChanges
中修改/添加/删除的实体,然后在
SavedChanges
中使用此信息来执行您需要对它们执行的操作。


0
投票

我也有同样的问题,所以你能帮我找到这门课的解决方案吗
保存后,它们现在不会更改,因为它们代表这些实体的数据库的当前状态。 如果您对此代码有更正,请与我联系以执行 这是 UpdateWorkflowInterceptor 类:

using Microsoft.AspNetCore.Mvc.Diagnostics;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Diagnostics;
using System.Data;
using System.Threading;
using Tsi.Erp.Domain.Entities;
using Tsi.SharedKernel.Infrastructure.BaseEntites.WorkflowEntites;
namespace Tsi.Erp.Infrastructure.Interceptors;

public sealed class UpdateWorkflowEntitiesInterceptor : SaveChangesInterceptor
{
    private ApplicationDbContext? dbContext;

    private IEnumerable<EntityEntry<ITsiWorkflowEntity<Guid>>>? entries;

    public UpdateWorkflowEntitiesInterceptor()
    {
    }

    public override async ValueTask<InterceptionResult<int>> SavingChangesAsync(DbContextEventData eventData,
        InterceptionResult<int> result,
        CancellationToken cancellationToken = default)
    {
        dbContext = eventData.Context as ApplicationDbContext;

        dbContext!.SavedChanges += (ob, ev) =>
        {
            Run();
            base.SavingChanges(eventData, result);
        };


        entries = dbContext
            .ChangeTracker
            .Entries<ITsiWorkflowEntity<Guid>>()
            .Where(e => e.State == EntityState.Added)
            .ToList();

        //     ListeInstance.AddRange( await CreateWorkflowInstanceAsync(entry.Entity));
            
        
        //dbContext.AddRange(ListeInstance);

        return await base.SavingChangesAsync(eventData, result, cancellationToken);
    }

    private void DbContext_SavedChanges(object? sender, SavedChangesEventArgs e)
    {
        Run();
    }
    private void Run()
    {
        if (entries is null) return;

        foreach (EntityEntry<ITsiWorkflowEntity<Guid>> entry in entries)
        {
             CreateWorkflowInstanceAsync(entry.Entity);
        }
        
    }

    private void  CreateWorkflowInstanceAsync(ITsiWorkflowEntity<Guid> entity)
    {
        
        var className = entity.GetType().Name;
        var (workflow, workflowEtat) = FindWorkflow(className);

        if (workflow != null && workflowEtat != null)
        {
            //var uidObjet = await FetchUidObjetFromDatabaseAsync();           
            var workflowInstance = new WorkflowInstance(className + "_" + DateTime.Now, workflow.Uid, DateTime.UtcNow, workflowEtat.Uid, entity.Uid);
            dbContext!.WorkflowInstance.Add(workflowInstance);
            entity.Workflow = workflow.Uid;

        }
        
    }


    private  (Workflow?, WorkflowEtat?) FindWorkflow(string className)
    {
        try
        {
            var workflow = dbContext!.Workflow
        .Include(w => w.WorkflowEtats)
        .FirstOrDefault(w => w.ClasswNavigation!.Nom.Equals(className));

            var workflowEtat = dbContext.WorkflowEtat
                .FirstOrDefault(w => w.WorkflowNavigation!.Uid.Equals(workflow!.Uid)
                                           && w.Uid == workflow.WorKflowEtatCreation);

            return (workflow, workflowEtat);
        }
        catch (Exception)
        {

            throw;
        }
    }
}

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.