Home > database >  Must contain a non-null value and possible null reference
Must contain a non-null value and possible null reference

Time:02-08

I'm trying to switch to .Net 6.0 from .Net 3.1 and I encounter a few issues.

Apparently, in .Net 6.0 models need to have non-nullable property, for example:

public string Email { get; set; } = String.Empty;

The problem for the DbSet I fixed like:

public DbSet<User>? Users { get; set; }

However, now I'm getting warnings of possible null reference argument/return as the image below:

possiblenull

CodePudding user response:

As the docs show you can use Set method with expression bodied property:

public DbSet<User> Users => Set<User>();

CodePudding user response:

Models don't "need to have non-nullable properties". It's just a suggestion.

If this is not the behavior you want you can open csproj file and either remove this line or make it disable

<Nullable>enable</Nullable>
  •  Tags:  
  • Related