1

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...

  • There are two potential sources of this error - first one is described [here](https://stackoverflow.com/a/65207398/2501279) and second one is due to the nullable reference types (see for example [here](https://stackoverflow.com/a/74477076/2501279)). – Guru Stron Jan 03 '23 at 22:34
  • @GuruStron I tried all: 1) to disable nullables with disable, 2) to have the Customer reference inside orders as `public Customer Customer { get; set; } = null!;` and also like `public Customer? Customer { get; set; }` but nothing works. Stills gives the error. – Efthymios Kalyviotis Jan 03 '23 at 22:45
  • @GuruStron explained on first comment (nullable navigation properties - left joins) cause the issue with EF linq select – SilentTremor Jan 04 '23 at 08:17
  • here is the exact why: https://github.com/dotnet/efcore/issues/20633 – SilentTremor Jan 04 '23 at 08:20
  • Did you find any solution? I've updaraded my app from 3.1 to 6 and there are many places we are getting this error. The one solution is to fix at every place. Do we have anything that we can do at one place? – Umer Waheed May 17 '23 at 10:49
  • 1
    @UmerWaheed Unfortunately, if I remember well we ended up fixing most of the errors manually. – Efthymios Kalyviotis May 17 '23 at 14:52

0 Answers0