繁体   English   中英

无法跟踪实体类型“x”的实例,因为已在跟踪另一个具有键值“{Id: 6}”的实例

[英]The instance of entity type 'x' cannot be tracked because another instance with the key value '{Id: 6}' is already being tracked

我正在做一个 asp .netcore 6.0 clean architecture 项目。

当我尝试更新网站时,出现此错误,

System.InvalidOperationException:无法跟踪实体类型“SiteCode”的实例,因为已在跟踪另一个具有键值“{Id: 6}”的实例。 附加现有实体时,请确保仅附加一个具有给定键值的实体实例。

在我们使用服务之前,同样的代码在那里运行良好。 现在我们转向清洁架构(CQRS 和 Mediatr)。 我使用了相同的更新代码,但我收到了这个错误。

.AsNoTracking()我尝试使用var result = await _DbContext.SiteCodes.FindAsync(request.Id).AsNoTracking(); 这条线,

但得到错误,

'ValueTask<SiteCode?>' does not contain a definition for 'AsNoTracking' and no accessible extension method 'AsNoTracking' accepting a first argument of type 'ValueTask<SiteCode?>' could be found (are you missing a using directive or an assembly reference?) [Application]

这是我的代码

UpdateSiteCommandHandler.cs

public async Task<SiteCode> Handle(UpdateSiteCommand request, CancellationToken cancellationToken)
    {
        var result = _mapper.Map<SiteCode>(request);

        _DbContext.SiteCodes.Update(result);  // goes to exception after this line

        await _DbContext.SaveChangesAsync(cancellationToken);

        return result;
    }

GetSiteByIdQueryHandler.cs

public async Task<SiteCode> Handle(GetSiteByIdQuery request, CancellationToken cancellationToken)
    {
        
        var result = await _DbContext.SiteCodes.FindAsync(request.Id);

        if (result == null)
        {
            throw new NotFoundException(nameof(SiteCode), request.Id);
        }

        return result;

    }

controller

public async Task<IActionResult> Update(int id, [FromBody] UpdateSiteCommand command)
        {
            command.Id = id;

            var siteCode = await _mediator.Send(new GetSiteByIdQuery(Id: id));

            var result = await _mediator.Send(command);
            
            return Ok(result);

        }

依赖注入.cs

public static class DependencyInjection
{
    public static IServiceCollection AddApplication(this IServiceCollection services)
    {
        services.AddAutoMapper(Assembly.GetExecutingAssembly());
        services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());
        services.AddMediatR(Assembly.GetExecutingAssembly());
        services.AddTransient(typeof(IPipelineBehavior<,>), typeof(PerformanceBehaviour<,>));  // I tried with AddScoped() But not work
       
        return services;
    }

}

在 DbContext 中

public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            var entries = ChangeTracker
                .Entries()
                .Where(e => e.Entity is AuditableEntity && (
                        e.State == EntityState.Added
                        || e.State == EntityState.Modified));

            foreach (var entityEntry in entries)
            {
                if (entityEntry.State == EntityState.Added)
                {
                    ((AuditableEntity)entityEntry.Entity).CreatedAt = DateTime.UtcNow;
                    ((AuditableEntity)entityEntry.Entity).CreatedBy = _httpContextAccessor?.HttpContext?.User?.Identity?.Name ?? null;
                }
                else
                {
                    Entry((AuditableEntity)entityEntry.Entity).Property(p => p.CreatedAt).IsModified = false;
                    Entry((AuditableEntity)entityEntry.Entity).Property(p => p.CreatedBy).IsModified = false;
                }

                ((AuditableEntity)entityEntry.Entity).ModifiedAt = DateTime.UtcNow;
                ((AuditableEntity)entityEntry.Entity).ModifiedBy = _httpContextAccessor?.HttpContext?.User?.Identity?.Name ?? null;
            }

            var result = await base.SaveChangesAsync(cancellationToken)

         return result;
        }

任何人都知道如何解决这个问题?

尝试将 SiteCodes 之后的 AsNoTracking() 方法添加到 Handler (GetSiteByIdQueryHandler.cs) 中的查询。

public async Task<SiteCode> Handle(GetSiteByIdQuery request, CancellationToken cancellationToken)
{
    
    var result = await _DbContext.SiteCodes.AsNoTracking().FindAsync(request.Id);

    if (result == null)
    {
        throw new NotFoundException(nameof(SiteCode), request.Id);
    }

    return result;
}

EF Core 使用 ChangeTracker 来检测加载实体的变化,更好(不是更快)的解决方案是加载实体进行更新。

public async Task<SiteCode> Handle(UpdateSiteCommand request, CancellationToken cancellationToken)
{
    // the following code will load entitiy if it is still not loaded.
    var dbRequest = await _DbContext.SiteCodes.FindAsync(request.Id);

    if (dbRequest == null)
        throw new Exception("Not found");

    var result = _mapper.Map<SiteCode>(request, dbRequest);

    await _DbContext.SaveChangesAsync(cancellationToken);

    return result;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM