My code is below, working fine in .NET Core 3.1:
// dto is search criteria passing from outside
var query = from p in dbContext.Products.Where(p => dto.Ids.Contains(p.Id))
from c in dbContext.Categorys.Where(c => c.Id == p.CategoryId).DefaultIfEmpty()
from wh in dbContext.Warehouses.Where(wh => wh.Id == p.WarehouseId).DefaultIfEmpty()
select new
{
Product = p,
Category = c,
Warehouse = wh
};
`var products = await query.ToListAsync();` // this line will throw exception
the result will contains some null for Category or Warehouse, in .NET Core 3.1 the default value will be assign to the object. But when running in .NET 7, it throws exception at run-time
System.InvalidOperationException with message "Nullable object must have a value."
2023-03-29T10:34:25.7553873Z at lambda_method3977(Closure, QueryContext, ValueBuffer)
2023-03-29T10:34:25.7555036Z at Microsoft.EntityFrameworkCore.InMemory.Query.Internal.InMemoryShapedQueryCompilingExpressionVisitor.QueryingEnumerable`1.Enumerator.MoveNextHelper()
2023-03-29T10:34:25.7555635Z at Microsoft.EntityFrameworkCore.InMemory.Query.Internal.InMemoryShapedQueryCompilingExpressionVisitor.QueryingEnumerable`1.Enumerator.MoveNextAsync()
2023-03-29T10:34:25.7559660Z at System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable`1.Enumerator.MoveNextAsync()
2023-03-29T10:34:25.7560134Z at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
2023-03-29T10:34:25.7560408Z at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
There are a lot of places like this through the whole project, check null manually almost an impossible task, can anyone help me give a solution for this please, just like how to turn back on the "setting default value when null" same as .NET 3.1 did before. Thank you.
p.Id
? – Dvinap
,c
, andwh
all beIEnumerable<>
types and not individual objects? – HydrodynamicsdbContext.Products.Where(...).Include(p => p.Category).Include(p => p.Warehouse)
– Illtreat