-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No way to .Include() multiple properties with AsNoTracking() Enabled #18570
Comments
Your include path contains cycle. TemplateQuestions -> Category -> Questions. In No tracking query we will materialize the Question entity again when loading that navigation, which will generate a different instance than the root instance. Hence generating incorrect results.
|
(I was leaving this to discuss in triage. Regardless of the answers to the questions, I think we need to provide some guidance here, and it's not clear to me what that guidance should be.) |
@ajcvickers The problem is that in 2.2 Ef Core it was possible to Include such thing as: Question Where template Includes all the Categories, that we want to process, in this case - process and then soft delete. .Include(c => c.Category.Template.Categories) Currently it results in a cycle if query is No-Tracking, as Template also includes the Category, that belongs to a question, from which we are started the tree.
I haven't checked it in this version, but Questions were not loaded as a part of Categories resolution in previous version we used. |
Note from triage: @smitpatel will show a workaround for no-tracking queries. |
Ping @smitpatel |
Verified same behavior with EF6:
|
Pinging @smitpatel |
1 similar comment
Pinging @smitpatel |
Model public class Blog
{
public int Id { get; set; }
public List<Post> Posts { get; set; }
}
public class Post
{
public int Id { get; set; }
public Blog Blog { get; set; }
} Query: var posts = db.Set<Post>().AsNoTracking().Include(p => p.Blog.Posts).Where(p => p.Id == 1).ToList();
// should be written to include from blog side
var query = db.Set<Post>().Where(p => p.Id == 1);
var blogs = db.Set<Blog>().Include(b => b.Posts).AsNoTracking()
.Where(b => query.Select(p => p.Blog.Id).Contains(b.Id))
.ToList();
var posts = blogs.SelectMany(b => b.Posts).Where(p => p.Id == 1).ToList(); |
I have the following scenario which worked fine in .net core 2.2 but in .net core 3.x and .net 5 I get the error: I have a location table with parent location, for example:
Now it doesn't work and I need to change all of queries. |
@offirpeer - EF Core stopped doing identity resolution for no tracking query since 3.x. See (breaking change)[https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.x/breaking-changes#notrackingresolution] If you want to do cycles in include then either do a tracking query or from 5.0 onwards you can also use AsNoTrackingWithIdentityResolution to go back to 2.2 behavior which will always work. |
I have a query, where I need to Include Multiple Navigation properties, that exist on the same level.
E.g. it can be done like so:
(the query is simplified to reflect only the problem)
Or rewritten like so:
Details:
In the current , 3.0 version of the Ef Core I started to get the following exception:
Is tracking really required, when handling multiple same-level properties of the Entity?
The text was updated successfully, but these errors were encountered: