abp
abp copied to clipboard
Entity Extensions (EF Core) generate incorrect SQL when deleting entities
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.
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
You can try to override the ApplyAbpConceptsForDeletedEntity method of AbpDbContext.
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);
}