Base solution for your next web application
Open Closed

Avoid save on unchanged entity #12649


User avatar
0
dominici created

From my tests (if correct), it seems that the ABP framework repository always saves even if none of the entity's properties have been changed. By default, EF Core doesn't save unchanged entities. Am I wrong? Is it possible that LastModificationTime handling causes this behavior?

Thanks, Fabrizio

Markdown is supported
Copy & paste or drag & drop images (max 30 MB per image)

4 Answer(s)
  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi @Fabrizio

    Your observation is correct if the update goes through Repository.Update/UpdateAsync(entity) in ASP.NET Boilerplate's EF Core integration.

    In that path, ABP attaches the entity and explicitly sets its state to Modified, so EF Core treats it as an update even if no business property has actually changed.

    So you are not wrong about EF Core itself: by default, EF Core does not send an UPDATE for an unchanged tracked entity. The difference here is that ABP's repository Update method forces the modified state.

    Regarding LastModificationTime: it is not what initially makes the entity modified. ABP sets LastModificationTime during SaveChanges, but only for entities that are already in Modified state. So it is more a consequence of the forced update path than the root cause.

    If the entity is first loaded/tracked and then saved without calling Repository.Update, EF Core's normal change tracking should apply, and no UPDATE should be sent when nothing has changed.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    dominici created

    Hi @oguzhanagir, thanks for the explanation.

    About: "In that path, ABP attaches the entity and explicitly sets its state to Modified, so EF Core treats it as an update even if no business property has actually changed."

    Currently, I'm managing manually the unchanged entities. Do you think it's possible to plan the implementation of this feature? Perhaps with a specific setting.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi @Fabrizio,

    Yes, this can be considered as an enhancement request.

    Currently, ASP.NET Boilerplate does not provide a built-in setting to make Repository.Update/UpdateAsync(entity) skip the database update when the entity has no actual property changes. The EF Core repository implementation intentionally marks the entity as Modified, so changing this default behavior would be a breaking behavioral change for existing applications.

    If we implement such a feature, it would most likely need to be opt-in, for example through a configuration setting. It also needs careful evaluation because it may affect detached entity updates, audit fields such as LastModificationTime, entity history, concurrency handling, and owned entity changes.

    For now, the recommended approach is still:

    • If the entity is already loaded/tracked, modify its properties directly and let EF Core change tracking decide whether an update is needed.
    • Avoid calling Repository.Update/UpdateAsync when you want EF Core’s normal unchanged-entity behavior.
    • Continue using manual checks/custom repository logic if you specifically need to skip forced updates for detached entities.

    We will share this with the team as a possible optional improvement; for now, we can plan this development for the next version.

    Thank you.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    dominici created

    Hi @oguzhanagir, I completely agree with you, this new feature need to be carefully evaluated and managed by configuration in order to avoid breaking changes. In the meanwhile, I continue to manage manually the unchanged entities.

    I hope to see this feature soon :-)

    Thank you

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)