entity-framework – Tracking vs. No-Tracking – Tracking and projections

  • Even if the result type of the query isn’t an entity type, if the
    result contains entity types they will still be tracked by default

Example :

  • In the following query, which returns an anonymous type, the
    instances of Book in the result set will be tracked

     using (var context = new BookContext())
     {
        var book = context.Books.Select(b => new { Book = b, Authors = b.Authors.Count() });
     }
    
  • If the result set does not contain any entity types, then no tracking
    is performed

Example :

  • In the following query, which returns an anonymous type with some of
    the values from the entity (but no instances of the actual entity
    type), there is no tracking performed.

    using (var context = new BookContext())
    {
       var book = context.Books.Select(b => new { Id = b.BookId, PublishedDate = b.Date });
    }
    

if you want to reproduce, please indicate the source:
entity-framework – Tracking vs. No-Tracking – Tracking and projections - CodeDay