-
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
Issue with AsNoTrackingWithIdentityResolution and JSON columns after upgrading to EF Core 9 #35468
Comments
Relevant change: 74d287f Using query operations on JSON collections with NTWIR was disabled, due to potential data corruption (e.g. when some elements of the collection are filtered out. We test for JsonProjectionInfo inside RelationalCollectionShaperExpression. However, Include should be safe - filtered include doesn't work with owned entities, so when we are inside include will always get all the json entities in the collection (i.e. the data corruption problem doesn't manifest) @lybe86 the only thing you can do for now is to break the query apart and stitch them on the client var children = await ctx.Children.Select(x => new { x, fk = EF.Property<Guid>(x, "ParentEntityId") }).AsNoTrackingWithIdentityResolution().ToListAsync();
var parents = await ctx.Parents.AsNoTrackingWithIdentityResolution().ToListAsync();
var stitched = parents.Select(x => new { x.Id, Children = children.Where(xx => xx.fk == x.Id).ToList() }).ToList(); but this workaround is not great. |
@maumar I guess my followup question is if this is something likely to change back or not, re-writing to stitch things together is ofcourse an option but also one i would like to avoid if I can simply postpone upgrading instead. |
@lybe86 I need to do a bit more investigation if Include is indeed safe to do in all cases - if so, the fix should be easy and likely available in EF10. When it comes to patching for EF9, I will discuss with the team. It's a regression and likely an easy fix, but at the same time it's a low priority/impact scenario - those often get rejected. Another option could be switching to regular tracking queries - what was your reason for using NTWIR instead? |
Well, one scenario we have is that we have some fairly dynamic rules that look through our userbase and do things to other parts of the system. So without NoTracking we risk something changing entities that should not be changed if anything sets any properties on a user or an attribute or whatever, low risk but still. Also if the user is already in the changetracker without included things we might get wrong data. So for instance one such chain we had was User -> AttributeValue -> AttributeDefinition Where the definition has some configuration about a specific attribute, and each user has lots of attributevalues but there are only a dozen or so attribute definitions. It is very possible that the pattern we are using is inefficient and that there are better ways doing things though such as shortening the chain and not have attribute definitions below each specific attribute value but having a lookup type of thing. |
Bug description
After upgrade from EFCore 8 to 9 i get an error when i use AsNoTrackingWithIdentityResolution whenever i include a collection of entities that has a JSON-column. I wrote a small console application with sqlite to show the problem.
With EF Core 8 this was not an issue and I could not find anything about this in the breaking changes section. Is there anything I can do to avoid this error, other than removing the JSON column or turning off identity resolution?
Your code
Stack traces
Verbose output
EF Core version
9.0.0
Database provider
sql/sqlite
Target framework
9.0
Operating system
No response
IDE
No response
The text was updated successfully, but these errors were encountered: