C
C#13mo ago
UltraWelfare

❔ EFCore Interceptor problem

In the MS documentation I see they're using the SaveChanges interceptor as:
public async ValueTask<InterceptionResult<int>> SavingChangesAsync(
DbContextEventData eventData,
InterceptionResult<int> result,
CancellationToken cancellationToken = default)
{
_audit = CreateAudit(eventData.Context);

using var auditContext = new AuditContext(_connectionString);

auditContext.Add(_audit);
await auditContext.SaveChangesAsync();

return result;
}
public async ValueTask<InterceptionResult<int>> SavingChangesAsync(
DbContextEventData eventData,
InterceptionResult<int> result,
CancellationToken cancellationToken = default)
{
_audit = CreateAudit(eventData.Context);

using var auditContext = new AuditContext(_connectionString);

auditContext.Add(_audit);
await auditContext.SaveChangesAsync();

return result;
}
I'm trying to do the same but I get an error and I presume it's because of the value task:
public override ValueTask<InterceptionResult<int>> SavingChangesAsync(DbContextEventData eventData, InterceptionResult<int> result,
CancellationToken cancellationToken = new CancellationToken())
{
if (eventData.Context is null) return result;

foreach (var entry in eventData.Context.ChangeTracker.Entries())
{
if (entry is not { State: EntityState.Deleted, Entity: ISoftDelete delete }) continue;
entry.State = EntityState.Modified;
delete.DeletedAt = DateTime.Now;
}

return result;
}
public override ValueTask<InterceptionResult<int>> SavingChangesAsync(DbContextEventData eventData, InterceptionResult<int> result,
CancellationToken cancellationToken = new CancellationToken())
{
if (eventData.Context is null) return result;

foreach (var entry in eventData.Context.ChangeTracker.Entries())
{
if (entry is not { State: EntityState.Deleted, Entity: ISoftDelete delete }) continue;
entry.State = EntityState.Modified;
delete.DeletedAt = DateTime.Now;
}

return result;
}
The error is that it cannot convert result to ValueTask.... how does the MS example work though...
2 Replies
UltraWelfare
UltraWelfareOP13mo ago
I'm dumb, I was missing the async keyword 🙂
Accord
Accord13mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server