Open Closed

The database operation was expected to affect 1 row(s), but actually affected 0 row(s); data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optim #4597


User avatar
0
Tisham.Ahuja created
  • ABP Framework version: v5.3.3
  • UI type: Blazor
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): Separated Identity server
  • Exception message and stack trace: The database operation was expected to affect 1 row(s), but actually affected 0 row(s); data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.
  • Steps to reproduce the issue:" below are the steps to reproduce error

Have this entity in domain project:

public class DepartmentAuditProductCopy : AuditedAggregateRoot<int>, ISoftDelete
{
    public int DepartmentAuditId { get; set; }
    public int ProductId { get; set; }
    public int Quantity { get; set; }
    public bool IsAdditionalSku { get; set; }
    public bool IsDeleted { get; set; }
}

DB set is added as below: public DbSet<DepartmentAuditProductCopy> DepartmentAuditProductCopy { get; set; }

Below is the code in DepartmentCopyDbContextModelCreatingExtensions

public static void ConfigureDepartmentCopy(
        this ModelBuilder builder)
{
    Check.NotNull(builder, nameof(builder));

    builder.Entity<DepartmentAuditProductCopy>(b =>
    {
        b.ToTable("DepartmentAuditProductCopy", DepartmentCopyDbProperties.DbSchema);
        b.ConfigureByConvention();
        b.Property(x => x.DepartmentAuditId).IsRequired();
        b.Property(x => x.ProductId).IsRequired();
        b.Property(x => x.Quantity).IsRequired();
        b.Property(x => x.IsAdditionalSku).IsRequired();
        b.Property(x => x.IsDeleted).IsRequired();
    });
}

Have below DTOs in Contracts project:

public class DepartmentAuditProductCopyDto : AuditedEntityDto<int>
{
    public int DepartmentAuditId { get; set; }
    public int ProductId { get; set; }
    public int Quantity { get; set; }
    public bool IsAdditionalSku { get; set; }
    public bool IsDeleted { get; set; }
}
public class CreateUpdateDepartmentAuditProductCopyDto : AuditedEntityDto<int>
{
    public int DepartmentAuditId { get; set; }
    public int ProductId { get; set; }
    public int Quantity { get; set; }
    public bool IsAdditionalSku { get; set; }
    public bool IsDeleted { get; set; }
}

Below is the mapper code: public DepartmentCopyApplicationAutoMapperProfile() { CreateMap<DepartmentAuditProductCopy, DepartmentAuditProductCopyDto>().ReverseMap(); CreateMap<DepartmentAuditProductCopy, CreateUpdateDepartmentAuditProductCopyDto>().ReverseMap(); }

Have below interface in contracts project

public interface IDepartmentAuditDetailsCopyAppService : ICrudAppService<
            DepartmentAuditProductCopyDto,
            int,
            PagedAndSortedResultRequestDto,
            CreateUpdateDepartmentAuditProductCopyDto>
{
    Task OperationSaveToDbIssue(List<DepartmentAuditProductCopyDto> records);
}

Below is the service code:

public class DepartmentAuditDetailCopyAppService : CrudAppService<
            DepartmentAuditProductCopy,
            DepartmentAuditProductCopyDto,
            int,
            PagedAndSortedResultRequestDto,
            CreateUpdateDepartmentAuditProductCopyDto>,
         IDepartmentAuditDetailsCopyAppService
{
    private readonly IRepository<DepartmentAuditProductCopy, int> _repository;

    public DepartmentAuditDetailCopyAppService(IRepository<DepartmentAuditProductCopy, int> repository) : base(repository)
    {
        _repository = repository;
    }

    public async Task OperationSaveToDbIssue(List<DepartmentAuditProductCopyDto> records)
    {
        try
        {
            var updateRecords = records.Where(rec => rec.Id != 0).ToList();
            var updateEntities = ObjectMapper.Map<List<DepartmentAuditProductCopyDto>, List<DepartmentAuditProductCopy>>(updateRecords);
            await _repository.UpdateAsync(updateEntities[0], true);
        }
        catch (System.Exception)
       {
            throw new UserFriendlyException("Some-Error-Occured");
        }
    }
}

When executing OperationSaveToDbIssue end point using below payload [ { "id": 1, "creationTime": "2023-02-24T05:23:00.294Z", "creatorId": "3fa85f64-5717-4562-b3fc-2c963f66afa6", "lastModificationTime": "2023-02-24T05:23:00.294Z", "lastModifierId": "3fa85f64-5717-4562-b3fc-2c963f66afa6", "DepartmentAuditId": 1, "productId": 1, "quantity": 1, "isAdditionalSku": true, "isDeleted": false } ]

The database operation was expected to affect 1 row(s), but actually affected 0 row(s); data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.


1 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You need to query the data first:

    try
    {
        var ids = records.Where(rec => rec.Id != 0).Select(rec => rec.Id).ToList();
        var updateRecords = await _repository..GetListAsync(x => ids.Contains(x.Id));
        foreach(var record in updateRecords)
        {
            ObjectMapper.Map(records.First(rec => rec.Id == record.Id) , record)
        }
        await _repository.UpdateManyAsync(updateRecords , true);
    }
    
Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11