1

I have this MyEntity table in the database:

MyEntity table in database

The data inside MyEntity is the following:

Data in MyEntity table

The EF Core entity is the following:

public class MyEntity
{
    public int Id { get; set; }
    public int MyInt { get; set; }
}

I am getting an exception:

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

when trying to load the MyEntity from the DbContext:

var myEntities = dbContext.Set<MyEntity>.ToList();

What am I doing wrong?

1 Answer 1

2

You are getting this error because the MyInt in your MyEntity database table is nullable, and one of the rows you are trying to load has a MyInt set to null.

To fix this, just change the type of your MyInt property in your entity to Nullable<int> or int?:

public class MyEntity
{
    public int Id { get; set; }
    public int? MyInt { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.