Efcore: Getting SqlNullValueException after upgrading to .Net Core 2.2

Created on 19 Jan 2019  Â·  17Comments  Â·  Source: dotnet/efcore

I was using .Net Core 2.1 and everything was fine. I just upgraded to .Net Core 2.2, and have started to see the following error: "SqlNullValueException: Data is Null. This method or property cannot be called on Null values."
My code lines are:

var sl = _context.Assets.OrderBy(e => e.Name); var slCnt = sl.Count(); var selectlist = new SelectList(sl, "AssetId", "Name").ToList();

On hitting the last of the 3 statements above, the code breaks with the said error. The 'sl' object is created, and I can get the count from the first two statements. Also, both AssetId and Name are non-null fields in the database, and I have checked to confirm that there is no null value in either column.

I should point out that all other SelectLists (for the other entities) work well even in 2.2. I am just not sure what is different with the Assets entity which has not changed since upgrading to 2.2.

Further technical details

EF Core version: 2.2
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017 15.9.5

closed-could-not-reproduce customer-reported

Most helpful comment

Also, EF can be configured (in OnModelCreating) to ignore the Required attribute and allow nulls.

All 17 comments

Facing the same issue, is there any work around? Just migrated to .net core 2.2 from 2.1 it was working fine there. I am using DBQuery to retrieve my data from sql view.

Following is the stack trace info:

  | Name | Value | Type
-- | -- | -- | --
  | StackTrace | " at System.Data.SqlClient.SqlBuffer.get_DateTime()\r\n at System.Data.SqlClient.SqlDataReader.GetDateTime(Int32 i)\r\n at lambda_method(Closure , DbDataReader , DbContext )\r\n at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider._FastQueryTEntity+MoveNext()\r\n at System.Collections.Generic.List1.AddEnumerable(IEnumerable1 enumerable)\r\n at System.Linq.Enumerable.ToListTSource in -------\SearchService.cs:line 82\r\n at ------Controllers.SearchController.Post(PagedSorted`1 parameters) in ------\Controllers\SearchController.cs:line 23\r\n at lambda_method(Closure , Object , Object[] )\r\n at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)\r\n at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)\r\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()" | string

@khatrizeeshan Please post a runnable project/solution or complete code listing that demonstrates the behavior you are seeing so that we can fully investigate.

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.

@ajcvickers Why did you close the issue? It's a serious problem, and I have the same problem.

@cryeshiren We haven't been able to reproduce. If you can post a small, runnable project/solution or complete code listing then we will will be able to fully investigate.

@dong have you made your date variable nullable?

I had the same issue and i made the above mentioned mistake.

On Mon, Apr 22, 2019 at 9:35 PM Arthur Vickers notifications@github.com
wrote:

Reopened #14467
https://github.com/aspnet/EntityFrameworkCore/issues/14467.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/aspnet/EntityFrameworkCore/issues/14467#event-2291666930,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABCQWYESSRH6BDCEVFDVLB3PRXSL5ANCNFSM4GREXOWA
.

@khatrizeeshan yes, I have some nullable field, mapping the allow null database field,
so, should I remove the nullable type?

I use some nullable guid and nullable DateTime in .net core 2.1, what should I do?
@ajcvickers @khatrizeeshan

This problem occurs with nullable guid or nullable datetime, in my project

ef core 2.2.2

This error happens when materializing query results is expecting non-null values but the value in results in database contains NULL. The cause of error could be bad data, incorrect model or bug in query pipeline to infer the type nullability correctly. Without a repro project we cannot determine the cause. Please post a runnable project which demonstrate the issue.

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.

I've experienced this issue when going from .NET Core 2.2 to 3-preview5 and it was caused by the fact I had abstract Entity class that contained [Required] attribute on nullableproperty. I would assume that same thing can happen if you configure required property via fluent API.

.Net Core 2.2 didn't seem to care and it worked perfectly fine, but the correct behavior is the one implemented in .NET Core 3 and this has admittedly been my mistake.

For reference I had something like:

public abstract class BaseEntityWithOptionalUserStamp<TEntity> : BaseEntity<TEntity>, IEntityWithOptionalUserStamp where TEntity : class, IEntity, new()
    {
        [Required]
        public int? CreatedByUserId { get; set; }

        public User CreatedByUser { get; set; }
    }

So, that means we cannot put validation attributes on our entities because that might break EF. Sigh. Ok, will have to do the validation on a separate viewmodel entities only.

So, that means we cannot put validation attributes on our entities because that might break EF.

That is incorrect conclusion. You can put Required attribute as needed but when reading value of such column from database it has to have a value. If it has null, it will throw exception. i.e. if data in database does not match the validation attributes you use then it is error.

Also, EF can be configured (in OnModelCreating) to ignore the Required attribute and allow nulls.

If it has null, it will throw exception. i.e. if data in database does not match the validation attributes you use then it is error.

Yes, that's exactly how I ended here - I put a [Required] on a nullable DateTime? property (it needs to be nullable to avoid being set to default value when no value received, leading to [Required] never working during HTTP POST validation scenarios) and then I got that exception when reading a list of entities from the database.

Also, EF can be configured (in OnModelCreating) to ignore the Required attribute and allow nulls.

Thanks for the hint, that might be useful.

This was my issue migrating from 2.2 to Core 3.0. spent far too much time figuring this out. It makes sense that [required] would mean non-nullable but I saw nothing in the change notes.

Thanks

Was this page helpful?
0 / 5 - 0 ratings