abp icon indicating copy to clipboard operation
abp copied to clipboard

Delete on ISoftDelete cause full update and fail

Open puschie286 opened this issue 5 years ago • 2 comments

GitHub Issues

  • Your ABP Framework version: 5.0 (dev)
  • Your User Interface: Blazor Server
  • Your database provider(EF Core/MongoDB): EF Core
  • Exception message and stack trace if available (check the logs).
  • Steps needed to reproduce the problem.

What happens: when you try to delete an entity/aggregateroot with ISoftDelete it will generate update query with ALL properties ( except primary key ). this cause a "Cannot update identity column '****'" error if it contains any identity columns.

AggregateRoot: ( only property definition )

public class RequestType : FullAuditedAggregateRoot<Guid>, IMultiTenant
{
	public Guid? TenantId { get; set; }
		
	public Guid? PermitOrderId { get; set; }
		
	public int AutoId { get; private set; }

	public string Name { get; private set; }
		
	public ApprovalTypeEnum ApprovalType { get; set; }
		
	public RequestOption Option { get; set; }

	public RequestOption Requirement { get; set; }
}

DB Context: ( extract )

builder.Entity<RequestType>( e =>
{
     e.ToTable( FS4Consts.DbTablePrefix + "RequestType", FS4Consts.DbSchema );
     e.ConfigureByConvention();

     // properties
     e.Property( x => x.Name )
         .HasMaxLength( LimitConsts.NameMaxLength )
         .IsRequired();

     e.Property( x => x.AutoId )
         .ValueGeneratedOnAdd()
         .IsRequired();
    [...]
}

Generated Query:

UPDATE [FS4RequestType] SET [ApprovalType] = @p0, [AutoId] = @p1, [ConcurrencyStamp] = @p2, [CreationTime] = @p3, [CreatorId] = @p4, [DeleterId] = @p5, [DeletionTime] = @p6, [ExtraProperties] = @p7, [IsDeleted] = @p8, [LastModificationTime] = @p9, [LastModifierId] = @p10, [Name] = @p11, [PermitOrderId] = @p1
2, [TenantId] = @p13
WHERE [Id] = @p14 AND [ConcurrencyStamp] = @p15;

What should happen: deletion of an entity/aggregateroot with ISoftDelete should update only 'IsDelete' ( and other delete related columns )

How to reproduce:

  1. Create an AggregateRoot with ISoftDelete ( f.e FullAuditedAggregateRoot ) with one int column ( f.e AutoId )
  2. Add AggregateRoot to DbContext and define int column as ValueGeneratedOnAdd
  3. Use AbpCrudPageBase with created AggregateRoot
  4. Try to delete any of the AggregateRoot

puschie286 avatar Nov 17 '21 09:11 puschie286

Hi,

Yes as you said it's updating whole entity when you delete it but it is not related with the ABP Framework, we just call the EF Core update method.

Also, this is not a problem for only "deleting", if you try to update your entity, you will get same error.

So, you should ignore the property on updating.

builder.Entity<RequestType>(e =>
{
     // other configurations
     e.Property(x => x.AutoId)
                    .ValueGeneratedOnAdd()
                    .IsRequired();

      // this will fix your problem.
      e.Property(x => x.AutoId).Metadata.SetAfterSaveBehavior(PropertySaveBehavior.Ignore);
}

cotur avatar Nov 25 '21 07:11 cotur

@cotur thanks for this suggesting but...

in AbpDbContext you set the EntityEntry.State to Modified on ApplyAbpConceptsForDeletedEntity which cause the entity framework to update all properties. ( Ref )

https://github.com/abpframework/abp/blob/2f1f79b8455b86e5b1816124ae5751b316b8743c/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs#L488-L489 I have prepared a PR for this

puschie286 avatar Nov 25 '21 12:11 puschie286