An exception occurred in the database while iterating the results of a query.
I have a table MyData with 3 columns of interest...
Name (string, not required) Description (string, not required) ID (long, required)
When running the following code:
string test = "26";
var res = _context.MyData.Where(
p => p.Name.Contains(test) ||
p.Description.Contains(test) ||
p.ID.ToString().Contains(test)
);
int count = res.Count();
EF generates the following SQL:
Opening connection to database 'blah' on server
SELECT [p].[ID], [p].[Description], [p].[Name] FROM [MyData] AS [p]
Closing connection to database 'blah' on server
and then blows up on res.Count() with
System.NullReferenceException: Object reference not set to an instance of an object.
at lambda_method(Closure , MyData)
at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source)
at lambda_method(Closure , QueryContext )
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass19_1`1.<CompileQuery>b__1(QueryContext qc)
Exception thrown: 'System.NullReferenceException' in Microsoft.EntityFrameworkCore.dll
The Description column does have some null values in it in the database.
If I remove the condition p.ID.ToString().Contains(test) from the Where clause then no exception is thrown because EF does the filtering on the SQL server by generating the following SQL:
[Parameters=[@__test_0='?' (Size = 4000), @__test_1='?' (Size = 4000)], CommandType='Text', CommandTimeout='30']
SELECT COUNT(*)
FROM [MyData] AS [p]
WHERE [p].[Name] LIKE (N'%' + @__test_0) + N'%' OR [p].[Description] LIKE (N'%' + @__test_1) + N'%'
It seems like the filtering should be done on the server in either case, by doing something like
WHERE CONVERT(varchar(max), ID) LIKE '%@TesVar%'
instead of pulling ALL records to the web server and then iterating over them locally... no?
Either way, I'm not sure what's up with the NullRef exception - that seems like an issue no matter what.
Further technical details
EF Core version: 1.0.0 Operating system: W10 Visual Studio version: 2015
Also, I'm about 85% sure this worked properly before v1.0.0.
Currently this is expected behavior. The Where clause is being evaluated in memory because EF Core doesn't know how to translate the p.ID.ToString() part to SQL. The p.Description.Contains(test) call will then result in a NullReferenceException when Description is null.
We have a work item to support ToString translation https://github.com/aspnet/EntityFramework/issues/6264.
We are also considering doing some magic to make things that evaluate in memory work more like they do in the database (which is much more flexible in terms of nulls), but we're not sure if we really want to go down that path.
BTW for the moment I would adjust the query to be something like...
_context.MyData
.Where(p => p.Name.Contains(test) || p.Description.Contains(test))
.Where(p => p.ID.ToString().Contains(test));
That way the first Where clause will evaluate in the database, and the nulls won't be an issue in SQL.
@rowanmiller Thanks!
Do I understand correctly that every time you use ToString(), EF will pull all records in a table (even if there are millions) to iterate over them locally?
Also, regarding your suggestion... doesn't your code change the the logic gate from
p.Name.Contains(test) || p.Description.Contains(test) || p.ID.ToString().Contains(test)
to
(p.Name.Contains(test) || p.Description.Contains(test)) && p.ID.ToString().Contains(test)
?
Do I understand correctly that every time you use ToString() , EF will pull all records in a table (even if there are millions) to iterate over them locally?
Correct. We log a warning whenever client evaluation occurs and you can also disable this all together if you want. See https://docs.efproject.net/en/latest/querying/client-eval.html for more info.
Also, regarding your suggestion... doesn't your code change the the logic gate
Ah yes, sorry I wrote that too quickly without thinking about it properly.
Thanks for that link! Had no idea that existed. That will at least make sure I catch them during development.
var roles = await userManager.GetRolesAsync(user);
var result = await userManager.RemoveFromRolesAsync(user, roles);
I get same error over here in my code Environment .net5.0 MVC APPLICATION
here is ex Deatil :An exception occurred while iterating over the results of a query for context type 'LogicLevel.AppDbContext