-
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
Query: NullReferenceException when property isn't marked as nullable but there is null data #6818
Comments
It also shows up when the fields in the projection are properly defined: https://gist.github.com/FransBouma/5d92ddcaa185a9f613b1f50081b28862 There, the SalesOrderHeader type is part of a projection from which the final projection is created, but it shouldn't be used for materialization, it will never happen. Or at least, it shouldn't. So the modeling mistake on SalesOrderHeader.SalesPersonID shouldn't be causing this query to crash but it does (with the same stacktrace). It's weird though, correcting the SalesPersonID mapping to make it nullable, should make the query in the Gist work, but it doesn't. All mappings should be OK now, no errors in your model checker nor in mine. Same exception. (additionally random note: you really should tell your users that DefaultIfEmpty is run in memory, normal users who don't enable full logging won't be aware of that and will pull lots of data in memory) |
@rowanmiller What does '1.2.0' mean btw? The query with the joins in the gist still crashes. I have no idea why (as in: is it my fault in the mappings or a bug in EF?) Will people have to wait till 1.2.0 hits the streets (which is in 2017 somewhere) ? What should I tell my customers who will use my designer, generate queries from their model, code which compiles fine and is accepted by your validator as OK, but crashes at runtime? That they have to wait till 1.2.0? |
The issue is marked for investigation, which means we want to dig into it a bit more. 1.1 is pretty much wrapped up, so assignment in 1.2 means it is on our "current list of work". We think that the NullReferenceException is fixed in 1.1. You should now get a better exception saying that we got a null from the database but we couldn't set it to the property because it didn't support nulls. Verifying this would be the first part of our investigation. One thing to clarify though...
This isn't how we have designed things. If we can't populate the property with the value from the database, then we will throw. If you have nulls in the database then you need to use a property type that is capable of holding them. |
Ok. I'll mark it in our docs as a warning for users, that generated queries by our designer might crash at runtime still, and refer to this issue. The join query shouldn't throw the null ref, as everything is properly defined for nulls.
But what if I have a left joined set and project it to a poco? Didn't EF6 use default(type) ? The thing is that the developer might test it with proper data, things work fine, but in practice in production things might crash with a null ref exception because due to a left join a null came back from the DB. It's up to you of course, but crashing out is IMHO not in the interest of the user, default(type) is. |
in case of left joins (e.g. when using optional navigation) we add null-compensation. Example:
(assuming there can be a customer without an address) will get translated to:
However, if the left-joined type is non-nullable (say, int) users need to help us with the possible null, either by casting the result to (int?) or using ?? operator:
|
I did some tests, my framework silently accepts it, EF6 also throws an exception about the non-nullability of an int (in case of left-join between customer and order in NW). IMHO, it's a burden to users, but it's your framework (and it doesn't have to cost any performance either) ;) It's a bit of a grey area, but anyway, thanks for clearing that up. I'll leave it open as the issue of the joined set is still unclear. |
NRE is indeed gone, now we tell the users that expected value was int, but got null instead. |
Steps to reproduce
On adventureworks, SalesOrderHeader.SalesPersonID, don't mark it as nullable, so give it as type: int
use simple fetch
NRE
The issue
Query above gives:
Full log:
This isn't really helpful:
Further technical details
EF Core version: 1.0.1, running on .NET full (4.5.2)
Operating system: windows 8.1
Visual Studio version: 2015
The text was updated successfully, but these errors were encountered: