dotnet / dotnet/EntityFramework.Docs

Projection queries are not tracked by default unless the complete record is a property of the result type

Open
#3,349 6 comments 1 reaction 0 assignees View on GitHub
Dominant language
Mermaid
Stars
1.7k
Forks
2k
Avg merge
7d 23h
Merged PRs (30d)
16

Description

I expected a projection query returning the entity type to be tracked because of these two sentences:
> By default, queries that **return entity types** are tracking.
> Even if the **result type** of the query isn't an **entity type**, EF Core will still track entity types contained in the result by default.

Tracking is not enabled for the projection query though. I can enable tracking by simply passing the result into the DbContext.Attach method. I think this is a bug with Entity Framework Core, but the documentation could be clearer on expected behavior.

```c#
var blog = context.Blogs
.Select(
b =>
new Blog { Id = b.Id, PostCount = b.Posts.Count() });
// changes to blog are not tracked
context.Attach(blog);
// changes to blog are now tracked
```

---
#### Document Details

⚠ *Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.*

* ID: f3ffc114-df51-0be7-8972-6fcead14e011
* Version Independent ID: 94c34571-7314-aab1-7898-93ac6d0f3430
* Content: [Tracking vs. No-Tracking Queries - EF Core](https://docs.microsoft.com/en-us/ef/core/querying/tracking)
* Content Source: [entity-framework/core/querying/tracking.md](https://github.com/dotnet/EntityFramework.Docs/blob/main/entity-framework/core/querying/tracking.md)
* Product: **entity-framework**
* Technology: **entity-framework-core**
* GitHub Login: @smitpatel
* Microsoft Alias: **avickers**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.