Код: Выделить всё
public class UpdateOperation(
IMapper mapper,
ApplicationDbContext dbContext,
ICurrentUtcTimeProvider currentUtcTimeProvider)
: IUpdateOperation
where TEntity : UserEntityBase
where TDto : DtoBase
{
private readonly IMapper _mapper = mapper;
private readonly ApplicationDbContext _dbContext = dbContext;
private readonly ICurrentUtcTimeProvider _currentUtcTimeProvider = currentUtcTimeProvider;
///
public async Task UpdateAsync(TEntity? existingEntity, TDto newDto, string userId)
{
if (existingEntity is null)
return;
_mapper.Map(newDto, existingEntity);
if (existingEntity is IUpdateHistory entityUpdateHistory)
{
entityUpdateHistory.UpdatedAt = _currentUtcTimeProvider.GetCurrentUtcTime();
entityUpdateHistory.UpdatedBy = userId;
}
_dbContext.Update(existingEntity);
await _dbContext.SaveChangesAsync();
}
}
Код: Выделить всё
public class UpdateInfoOperation(
ICurrentUtcTimeProvider currentUtcTimeProvider)
: IUpdateInfoOperation
{
private readonly ICurrentUtcTimeProvider _currentUtcTimeProvider = currentUtcTimeProvider;
///
public void UpdateInfo(IUpdateHistory entity, string userId)
{
ArgumentNullException.ThrowIfNull(userId);
entity.UpdatedBy = userId;
entity.UpdatedAt = _currentUtcTimeProvider.GetCurrentUtcTime();
}
}
Код: Выделить всё
///
public async Task UpdateAsync(Guid id, RailLineDto newDto, string userId)
{
RailLine? existingEntity = await FindEntityByIdAsync(id, userId);
if (existingEntity == null)
return;
_mapper.Map(newDto, existingEntity);
_updateInfoOperation.UpdateInfo(existingEntity, userId);
_dbContext.Update(existingEntity);
await _dbContext.SaveChangesAsync();
}
///
/// Asynchronously finds a RailLine entity by its identifier and user ID.
///
///
The ID of the RailLine entity to find.
/// The ID of the user who owns the entity.
/// A task that represents the asynchronous operation. The task result contains the RailLine entity if found; otherwise, null.
private async Task FindEntityByIdAsync(Guid id, string userId)
{
return await
_dbContext.RailLines
.Include(l => l.Stations)
.Include(l => l.SpeedSections)
.Include(l => l.Gradients)
.Include(l => l.Curves)
.Include(l => l.Tunnels)
.Include(l => l.ElectrifiedSections)
.Include(l => l.Substations)
.FirstOrDefaultAsync(l => l.Id == id && l.UserId == userId && !l.IsDeleted);
}
Я попробовал вручную переключить EntityState на Modified, но это не сработало. Мне код кажется таким же, и я только переместил его в другое место, так что проблем быть не должно. Или я что-то упускаю?
Подробнее здесь: https://stackoverflow.com/questions/792 ... -db-and-at