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

The error message indicates that EF Core is trying to read string 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).

Looking at your entity model and corresponding database table, you can see the obvious discrepancy for many string properties -> varchar columns. CompanyStreetAddress, CompanyCity, CompanyZipCode, CompanyVatNumber, ContactFirstName, ContactLastName – all these are marked as [Required] in the model, but have no corresponding not null constraint in the table.

So the problem is caused by one or more of these columns.

You need to fix that discrepancy – probably by removing [Required] attribute because the constraint is already broken in the existing data.

The fact that it “works” in some older EF Core version doesn’t matter – that’s incorrect mapping and as such should be fixed. Technically it shouldn’t work from the beginning. But remember that EF Core is still in active development and has many bugs which are fixed in the next release(s). Most likely some code change was made between “working” and “non working” EF Core version which fixes the previous incorrect behavior.

Leave a Comment