Currently I'm doing the following when inserting/updating and entity:
public async Task<IdentityResult> UpdateAsync(Model model, CancellationToken ct = default)
{
var entity = await GetAsync(ct);
if (entity == null)
{
return await CreateAsync(model, ct);
}
context.Entry(entity).State = EntityState.Detached;
Update(model);
model.ConcurrencyToken = Guid.NewGuid().ToString();
await context.SaveChangesAsync();
return IdentityResult.Success;
}
And the update method:
protected virtual T Update(T entity)
{
var dbEntityEntry = context.Entry(entity);
if (dbEntityEntry.State == EntityState.Detached)
{
dbSet.Attach(entity);
dbEntityEntry.State = EntityState.Modified;
}
return entity;
}
This is working. But is there a better (generic) way to handle concurrency?