1

I am upgrading to Entity Framework Core 6.0.7 and when reading from my database I am getting an error. All of this code was working perfectly before the upgrade.

When calling this method public virtual IQueryable<T> GetAll() => _dataContext.Set<T>(); I am getting the following error

Microsoft.EntityFrameworkCore.Query[10100] An exception occurred while iterating over the results of a query for context type 'Data.DbContext'. System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values.

Generic type <T> in this instance is a GameVariant class.

public class GameVariant : IEntityBase, IActiveEntity
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string IconUrl { get; set; }
    public bool Active { get; set; } = true;
}

Looking at my GameVariant database table I can see lots of records in there. But I noticed that some of the Description and IconUrl values are NULL. So I changed the Description and IconUrl properties on the GameVariant class to be string? instead of simply string.

Now when calling the GetAll() method data is being retrieved fine and the error is no longer being thrown. Can anyone tell me why changing string to string? resolves this issue? I thought string was nullable by default?

Also what is the best method for me to fix this issue without having to go through every single database class and update every string property?

2

1 Answer 1

8

From C# 8.0 this change was introduced. Short explanation is this one:

Prior to C# 8.0, all reference types were nullable. Nullable reference types refers to a group of features introduced in C# 8.0 that you can use to minimize the likelihood that your code causes the runtime to throw System.NullReferenceException. Nullable reference types includes three features that help you avoid these exceptions, including the ability to explicitly mark a reference type as nullable:

- Improved static flow analysis that determines if a variable may be null before dereferencing it.
- Attributes that annotate APIs so that the flow analysis determines null-state.
- Variable annotations that developers use to explicitly declare the intended null-state for a variable.

For lengthy explanation check: https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references

You can disable this on project level, in .csproj file just add this:

<PropertyGroup>
  <Nullable>disable</Nullable>
</PropertyGroup>

You can also check this, but note that it is closed due to be opinion based: Deal with in Net 6's nullable and Entity Framework entities

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.