abp icon indicating copy to clipboard operation
abp copied to clipboard

Entity Extensions (EF Core) generate incorrect SQL when deleting entities

Open cdlian opened this issue 2 years ago • 1 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Description

In my example code, there are two extended properties: DbProperty is mapped to a database field through EFCore and cannot be NULL, while NoDbProperty is stored in ExtraProperties. When I delete an entity, the generated SQL set DbProperty to NULL.

iShot_2024-09-02_10 58 25 iShot_2024-09-02_11 00 52

Sample Code

Reproduction Steps

No response

Expected behavior

No response

Actual behavior

No response

Regression?

No response

Known Workarounds

No response

Version

8.3.0-rc.3

User Interface

MVC

Database Provider

EF Core (Default)

Tiered or separate authentication server

None (Default)

Operation System

macOS

Other information

No response

cdlian avatar Sep 05 '24 01:09 cdlian

You can try to override the ApplyAbpConceptsForDeletedEntity method of AbpDbContext.

image

protected override void ApplyAbpConceptsForDeletedEntity(EntityEntry entry)
{
    if (!(entry.Entity is ISoftDelete))
    {
        return;
    }

    if (IsHardDeleted(entry))
    {
        return;
    }

    ExtraPropertyDictionary? originalExtraProperties = null;
    if (entry.Entity is IHasExtraProperties)
    {
        originalExtraProperties = entry.OriginalValues.GetValue<ExtraPropertyDictionary>(nameof(IHasExtraProperties.ExtraProperties));
    }

    entry.Reload();

    if (entry.Entity is IHasExtraProperties)
    {
        ObjectHelper.TrySetProperty(entry.Entity.As<IHasExtraProperties>(), x => x.ExtraProperties, () => originalExtraProperties);
    }

    ObjectHelper.TrySetProperty(entry.Entity.As<ISoftDelete>(), x => x.IsDeleted, () => true);
    SetDeletionAuditProperties(entry);
}

maliming avatar Sep 05 '24 05:09 maliming