Nullable exception after updating to Preview-8 (Nullable object must have a value)
See original GitHub issueIn case of move to last version of ef core (5.0.0-preview.8.20407.4), app throws exceptions: “nullable object must have value” for all objects that contains some nullable defined member…
All older versions works only prev-8 not… ( tested on prev 5,6,7 as OK )
- Tested with different DBs (SQL/SQLite/Postgres) (So i think it is not provider related…)
- Tested that this is not migration or tools related…
This happens when any object has some Nullable member which is null… Im adding example for UserRole Null…
Example DB model:
public class User {
public long ID { get; set; }
public string UserName {
get {
return String.Format("{0}.{1}", FirstName, LastName);
}
}
#nullable enable
public string? AvatarFileName { get; set; }
#nullable disable
public string FirstName { get; set; }
public string LastName { get; set; }
#nullable enable
public string? Email { get; set; }
#nullable disable
public User() {
this.Tokens = new HashSet<RefreshToken>();
this.Groups = new HashSet<GroupUser>();
this.Claims = new HashSet<UserPermission>();
this.Email = "";
this.AvatarFileName = "";
}
#nullable enable
public DateTime? ActivTimestamp { get; set; }
#nullable disable
public bool Deactivated { get; set; }
virtual public IEnumerable<RefreshToken> Tokens { get; set; }
#nullable enable
virtual public Role? Role { get; set; }
#nullable disable
virtual public IEnumerable<GroupUser> Groups { get; set; }
....
....
etc.
}
From SELECT:
SELECT "t"."ID", "t"."Email", "t"."FirstName", "t"."LastName", "r"."ID", "r"."Name", "r"."Description", "r"."Code", "t"."ActivTimestamp", "t"."AvatarFileName" AS "AvatarImageName", "t"."Deactivated"
FROM (
SELECT "u"."ID", "u"."ActivTimestamp", "u"."AvatarFileName", "u"."Deactivated", "u"."Email", "u"."FirstName", "u"."LastName", "u"."RoleID"
FROM "Users" AS "u"
WHERE ("u"."FirstName" IS NOT NULL AND NOT ("u"."Deactivated")) OR ("u"."LastName" IS NOT NULL AND NOT ("u"."Deactivated"))
LIMIT @__p_0
) AS "t"
LEFT JOIN "Roles" AS "r" ON "t"."RoleID" = "r"."ID"
Throw:
An exception occurred while iterating over the results of a query for context type 'Presistence.AppDbContext'.
System.InvalidOperationException: Nullable object must have a value.
at lambda_method(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
System.InvalidOperationException: Nullable object must have a value.
at lambda_method(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
System.InvalidOperationException: Nullable object must have a value.
at lambda_method(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )
...
...
etc.
Versions: EF Core version: 5.0.0-preview.8.20407.4 Database provider: SqlServer or SQL or PosgreSQL (tested with all so i think it is not provider related…)
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Nullable object must have a value?
The value coming from the database is a nullable boolean. When you call Nullable.Value, and the value is null, you will get this...
Read more >Nullable Object Must Have a Value: Causes and Solutions
Nullable object must have a value is an “InvalidOperationException” shown by the compiler in the C Sharp programming language (C#) when you don't...
Read more >[Solved] Nullable object must have a value
It depends what you want to do when the date is null. You can check if it is null via. C#. Resignation_Date.HasValue
Read more >"Nullable object must have a value" error is observed after ...
An error with message "Nullable object must have a value" is observed after upgrading Sitefinity to version 12.0. 7025 and above. Cause 2:...
Read more >Unexpected Error: Nullable object must have a value.
If it does, just close the error window. In the Power Query Editor, click on the 'Home' ribbon tab, and then click the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You need to check for
e.Role
being null then returning null rather than create the Role_DTO.@smitpatel thx for info… Oh yes i see… So whats the workaround for this? or better say how to do this select with new prev8 ? i saw the merge is new and something going back to .net core 2.2 behavior…
For test when i change in Role_DTO
public long? ID { get; set; }
as nullable it will work but is this the way of handle ? To have all iDs as nullable in all DTOs ? thx