EfCore REST-API:对于无状态应用程序,查询应始终为 AsNoTracking

EfCore REST-API: For stateless application, should query always be AsNoTracking

REST 是无状态的,EfCore 在查询时默认跟踪实体。我相信 EfCore 在没有跟踪的情况下表现更好。

在 web-api 中,对于任何 Http_Get,我使用 dbContext.Set<TEntity>().AsNoTracking(); 来获取 IQueryable 而不是 dbContext.Set<TEntity>().AsQueryable();。 但是在某些情况下我必须跟踪实体,例如更新断开连接的实体时。

对所有 Http_GET 请求进行无跟踪查询是一种好方法吗?

是的,使用 .AsNoTracking() 进行只读查询是一个很好的方法。

看到这个documentation entry:

No tracking queries are useful when the results are used in a read-only scenario. They're quicker to execute because there's no need to set up the change tracking information. If you don't need to update the entities retrieved from the database, then a no-tracking query should be used.