-
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
ChangeTracker.Entries() throws NullReferenceException #6157
Comments
@xrkolovos Can you please post code for the entity types that are being used? Also, if the application is adding more than just a single entity, then can you post code that reproduces how the entities are being added? |
I actually managed to make it work. |
@xrkolovos Glad you got it working, but there still should not be any case where this code throws null-ref exception, so if you could help me reproduce the issue that would be great. |
@ajcvickers it's complicate to find a way to reproduce it. I must give you all the source code for my model layer and repository layer and add you sample console app to trigger it. |
We're not too concerned about this one as it sounds like a very difficult situation to get into. If you end up supplying a way to reproduce it, then we'll look into it, but we're not too concerned if you can't. |
@ajcvickers looks like I spot the same issue.
|
The problem here is that EF is not aware of the change made to CustomerId. When a change is made to an entity, it is a requirement that the application make EF aware of this change before performing any further operations with the context. In this case there are three main ways of doing this:
Any one of these will result in correct behavior. The problem with any other approach is that detecting changes can be slow, which means if we do it automatically in lots of places, then EF perf will get much worse. So we choose to call DetectChanges automatically only in the places where it adds most value. Typically before a new query is not one of those places. The behavior if the context is not made aware of some change to an entity is undefined. However, in this case it is possible to be more resilient to a null relationship snapshot. I will submit a PR for that, |
Note for triage: RelationshipsSnapshot can be null when DetectChanges (or something equivalent) has not been used when it was needed. This is an application bug (not calling DetectChanges or similar) and the behavior of EF is undefined. However, in some cases the application may work fine because the FK is correct and the lack of fixed up navigation property is not significant to EF. Three possible resolution paths come to mind for this case:
|
Consensus is that we go with option 1 |
Issue #6157 See issue for full discussion.
Issue #6157 See issue for full discussion.
Steps to reproduce
Realy don't know
The issue
I have this code
And when i try to insert something random i get this error. Any clue?
Further technical details
EF Core version: 1.0.0
Operating system: win10
Visual Studio version: VS 2015
The text was updated successfully, but these errors were encountered: