![](/img/trans.png)
[英]Error: The instance of entity type 'X' cannot be tracked because another instance with the key value '{ID: 3}' is already being tracked
[英]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.