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
• I have an entity in ModuleA as below: public class ProductOneColumn { [Key] public int Id { get; set; } public string ProductCode { get; set; } }
• Added DB set for this in ModuleADbContext public DbSet<ProductOneColumn> ProductOneColumn { get; set; }
• Below is the code in ModuleADbContextModelCreatingExtensions builder.Entity<ProductOneColumn>(b => { b.ToTable("ProductOneColumn", ProductConstants.DbSchema); b.ConfigureByConvention(); b.Property(x => x.ProductCode).HasMaxLength(8); });
• I have exactly the same entity in ModuleB as well (This entity will also use same DB table being used by ProductOneColumn entity in ModuleA): public class ProductOneColumn { [Key] public int Id { get; set; } public string ProductCode { get; set; } }
• Added DB set for this in ModuleBDbContext public DbSet<ProductOneColumn> ProductOneColumn { get; set; }
• Below is the code in ModuleBDbContextModelCreatingExtensions
builder.Entity<ProductOneColumn>(b => { b.ToTable("ProductOneColumn", FedExConstants.DbSchema, t => t.ExcludeFromMigrations()); b.ConfigureByConvention(); b.Property(x => x.ProductCode).HasMaxLength(8); });
Note: Two different entities in both the modules are trying to access the same table and Only difference between code in both the modules is that in ModuleBDbContextModelCreatingExtensions I have added b.ToTable("ProductOneColumn", FedExConstants.DbSchema, t => t.ExcludeFromMigrations()); instead of b.ToTable("ProductOneColumn", ProductConstants.DbSchema);
• In AppDBContext I have 2 method calls (this code was automatically added when I added modules) builder.ConfigureModuleA(); builder.ConfigureModuleB();
Now when I run API and click on authorize, I get below error: InvalidOperationException: Cannot use table 'ProductOneColumn' for entity type 'ProductOneColumn' since it is being used for entity type 'ProductOneColumn' and potentially other entity types, but there is no linking relationship. Add a foreign key to 'ProductOneColumn' on the primary key properties and pointing to the primary key on another entity type mapped to 'ProductOneColumn'.
• If I comment out builder.ConfigureModuleB(); then It works. However I am not aware of impact of commenting out this line of code.
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.