myesn

myEsn2E9

hi
github

ABP: EF Core AsNoTracking()

After reading the Abp Unit Of Work document, it is known that the following method types will be considered as a unit of work:

  • ASP.NET Core MVC Controller Actions.
  • ASP.NET Core Razor Page Handlers.
  • Application service methods.
  • Repository methods.

For these methods, UOW will be automatically started unless a UOW is already running around (in the environment).

In simple terms, the above 4 types of methods will automatically start a transaction before execution, and after successful execution without exceptions, the database transaction will be automatically committed.
This means that in methods like GetListAsync or GetAsync, if the values of entity properties are changed, even if UpdateAsync is not called to update the entity, the changes will be synchronized to the database after the method is successfully executed, thanks to transactions.
Therefore, we need to disable EFCore's entity change tracking feature to prevent unintentional changes to entity values in read-only methods. The method of disabling is encapsulated by ABP, refer to: https://github.com/abpframework/abp/issues/17487

However, indeed, even when querying data in read-only methods, entity property values should not be changed. Instead, the values of the DTO should be changed, so there is no need to DisableTracking.

References#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.