pinale
pinale

Reputation: 2214

Entity Framework Core 6 - System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values

I'm using EF Core 6 on .NET 6.0 and am looking at this error:

System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values

The error message should indicate that EF Core is trying to read a value for a required property, i.e. a property which should never have null value in the database, but instead the underlying data reader reports null value for that property in some record(s).

Entity Framework Core: `SqlNullValueException: Data is Null.` How to troubleshoot?

Disabling nullable reference type the error disappears and everything works well.

Is it possible to investigate exactly what field is causing this issue, debugging the code doesn't give me more details.

Is there some technique to get the name of the offending field or fields?

Visually I didn't find any discrepancy between the database and the model

Upvotes: 0

Views: 2640

Answers (2)

Ruikai Feng
Ruikai Feng

Reputation: 11939

Check the document related with Nullable reference types

In a nullable aware context:

A variable of a reference type T must be initialized with non-null, and may never be assigned a value that may be null.

A variable of areference type T? may be initialized with null or assigned null, but is required to be checked against null before de-referencing.

assuming an entity:

public class SomeEntity   
{   
    [Key]     
    public int Id { get; set; }       
    public string Name { get; set; }        
    public string Prop { get; set; }    
}

If you enabled nullable reference type, Name and Prop property would never be assigned with null

Assuming the Db:

enter image description here enter image description here

When you read data from db and assign the null value in db to your entity,you would get the error you've shown

Is it possible to investigate exactly what field is causing this issue

All properties type of T instead of T? in your entity with the column could be null in db would cause this issue

Both Disabling nullable reference type and Setting your property which may have null value in db with T? would solve the issue.

For example,in my case,the Name column and Prop column could be null in db,when you check your entity, Name and Prop property should be type of string? instead of string .

Upvotes: 1

pinale
pinale

Reputation: 2214

yes, this is the cause. But my question (maybe is not so clear, it's my falut) is about how to get some sort of notification from the debbugger, the call stack, or from a verbose output, an indicatin of what field has null value

It's not so easy to verify manually because my entity are a customization of a the aspnet identity database

E.G: calling _userManager.FindByNameAsync i got this error

Apparently i've filled all nullable values but maybe the error is caused by other related entity e.g Roles, Claims, and so on

So i'd like to get a clear indication about what is the first field(if there are more) that has caused the problem.

If possible of course!

Upvotes: 0

Related Questions