How to combine Find() and AsNoTracking()?

So instead of using AsNoTracking() what you can do is Find() and then detach it from the context. I believe that this gives you the same result as AsNoTracking() besides the additional overhead of getting the entity tracked. See EntityState for more information.

var entity = Context.Set<T>().Find(id);
Context.Entry(entity).State = EntityState.Detached;
return entity;

Edit: This has some potential issues, if the context hasn’t loaded some relationships, then those navigation properties will not work and you will be confused and frustrated why everything is returning null! See for more info. For now on those repositories I’m overriding the FindNoTracking() methods in my repositories that I need that in.

Leave a Comment