In .net core 3.1 I had the following expression:
var result = _dbContext.Orders.Where(x => x.OrderId == orderId)
.Select(x =>
new
{
x.Customer.CustomerId,
x.ProductId,
x.Quantity,
}).FirstOrDefault();
Everything was working fine. After we updated to .Net 6 with the latest EntityFramework core 6.x we started receiving the error Nullable object must have a value after updating to .Net 6.
I know that this error is because the Order table rows have sometimes customers which are null (not pointing to a customer).
So, the x.Customer.CustomerId
inside the anonymous object gives this error.
I also know that if I convert that expression to the below, then it will operate correctly.
var result = _dbContext.Orders.Where(x => x.OrderId == orderId)
.Select(x =>
new
{
CustomerId = (int?)x.Customer.CustomerId,
x.ProductId,
x.Quantity,
}).FirstOrDefault();
Can someone tell me please what changed to the .Net 6 and/or EFCore 6 and the previous statement doesn't operate any more? Can you please let me know if there is any change that I can make (either globally or at least in the DB model) in order to avoid such errors?
It will be difficult for me to do changes one by one as there are hundreds of such expressions we are using. As such I need a change to be done globally...