BEST
DEALS
OF THE
YEAR!
SAVE UP TO $3,000
LAST DAY 1 DEC
00 Days
00 Hrs
00 Min
00 Sec
Open Closed

Audit entity when change foreign keys #10071


User avatar
0
maria_ruiz created

Good morning,

We have a problem with the ABP audit.

The situation is as follows:

We have entity A (aggregate) which, if we modify related properties (foreign key), is not being updated in the LastModificationTime column of entity A.

It should show today's date, 2025-11-05.

Therefore, AbpEntityChanges is not recorded with the correct date.

We have overwritten your code and noticed that you have this if statement. Is there a specific reason for this?


3 Answer(s)
  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent
    1. Solution
    • What you observe is by design in ABP Framework versions before 8.3. In AbpDbContext.PublishEventsForTrackedEntity, ABP intentionally skips “updated” entity events and ABP concept application when only foreign key properties are modified:

      • If all modified properties are metadata.IsForeignKey(), it breaks without calling ApplyAbpConceptsForModifiedEntity and without publishing EntityUpdated events. Consequently:
        • LastModificationTime/LastModifierId aren’t updated.
        • EntityUpdated event and AbpEntityChanges may not reflect the parent aggregate update timestamp as you expect.
    • Reason: This optimization existed to avoid firing update logic when only relationship fixups (FK changes) happen and the entity’s own scalar properties didn’t change.

    • Starting from ABP 8.3, this behavior was changed: ABP applies auditing/concurrency updates when a navigation property changes. That means LastModificationTime, LastModifierId, ConcurrencyStamp and EntityVersion are updated when navigation (and thus FK) changes are detected.

    • Your project is on ABP 9.1.3, which already includes that change. To ensure it’s active:

      1. Make sure you’re using the EF Core integration (Volo.Abp.EntityFrameworkCore) that contains the updated AbpDbContext. The AbpDbContext implementation on the dev branch (and since 8.3) applies ABP concepts when navigation changes occur.
      2. Ensure the option that controls publishing for navigation changes is not disabled. ABP provides an option on the local event bus side:
        • AbpEntityChangeOptions.PublishEntityUpdatedEventWhenNavigationChanges should be true (default). If you had set it to false anywhere, EntityUpdatedEvent will not be published when navigation changes, which can also affect auditing/event emission behavior. Example:
    Configure<AbpEntityChangeOptions>(o =>
         {
             o.PublishEntityUpdatedEventWhenNavigationChanges = true; // default
         });
    
    1. Verify that your FK change is tracked as a navigation change within the same DbContext unit of work. If you’re using a read-only/no-tracking repository or manually turned off tracking, EF Core won’t report the modification and ABP can’t apply modification audit properties.
      • Use IRepository (tracking) for updates; avoid IReadOnlyRepository or NoTracking queries when you intend to modify.
    2. Do not override AbpDbContext.PublishEventsForTrackedEntity to restore the old “skip FK-only” branch. If you have customized AbpDbContext and re-introduced the condition you highlighted (skipping when all modified properties are foreign keys), remove that branch so ABP can apply modification audit properties and publish the update.
    3. Make sure entity change auditing is enabled if you rely on AbpEntityChanges rows:
    Configure<AbpAuditingOptions>(options =>
         {
             options.EntityHistorySelectors.AddAllEntities();
         });
    
     Note: Even without entity history, the LastModificationTime should be set by ApplyAbpConcepts once navigation/FK changes are considered updates.
    
    • If you must keep older behavior (pre-8.3) or selectively control it:

      • You can re-add the FK-only skip in a derived DbContext (not recommended), or set PublishEntityUpdatedEventWhenNavigationChanges = false to stop EntityUpdated events for navigation changes. This will again stop LastModificationTime updates tied to those events. The recommended path is to keep the default behavior introduced in 8.3.
    • Summary action items for your case:

      • Remove/undo the “skip if all modified are foreign keys” code in your overridden DbContext.
      • Ensure PublishEntityUpdatedEventWhenNavigationChanges is true (default).
      • Ensure tracking is enabled for the update UoW.
      • With ABP 9.1.3, FK-only changes should update LastModificationTime and be captured in audit changes as per the new behavior.
    1. Sources
    • https://abp.io/docs/latest/framework/infrastructure/audit-logging
    • https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs
    • https://abp.io/docs/latest/release-info/migration-guides/abp-8-3
    • https://abp.io/docs/latest/framework/infrastructure/event-bus/local/

    This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.

  • User Avatar
    0
    maria_ruiz created

    This is the code of the 9.1.3 version This is the url of your code: https://github.com/abpframework/abp/blob/9.1.3/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    We enhanced it in the next 9.3.x version

    You can override the method to change it now.

    See

    https://abp.io/support/questions/10057/LastModification-fields-not-updated-when-changing-one-to-many-relationship#answer-3a1d5bfa-d803-667e-e95a-7f8900a6abc8 https://github.com/abpframework/abp/pull/24104

    Thanks

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.1.0-preview. Updated on November 20, 2025, 09:12
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.