diff --git a/entity-framework/core/miscellaneous/nullable-reference-types.md b/entity-framework/core/miscellaneous/nullable-reference-types.md index dbe659061e..723871d54f 100644 --- a/entity-framework/core/miscellaneous/nullable-reference-types.md +++ b/entity-framework/core/miscellaneous/nullable-reference-types.md @@ -78,7 +78,11 @@ Another strategy is to use non-nullable auto-properties, but to initialize them When dealing with optional relationships, it's possible to encounter compiler warnings where an actual `null` reference exception would be impossible. When translating and executing your LINQ queries, EF Core guarantees that if an optional related entity does not exist, any navigation to it will simply be ignored, rather than throwing. However, the compiler is unaware of this EF Core guarantee, and produces warnings as if the LINQ query were executed in memory, with LINQ to Objects. As a result, it is necessary to use the null-forgiving operator (!) to inform the compiler that an actual `null` value isn't possible: -[!code-csharp[Main](../../../samples/core/Miscellaneous/NullableReferenceTypes/Program.cs?name=Navigating)] +```csharp +var order = context.Orders + .Where(o => o.OptionalInfo!.SomeProperty == "foo") + .ToList(); +``` A similar issue occurs when including multiple levels of relationships across optional navigations: diff --git a/samples/core/Miscellaneous/NullableReferenceTypes/Program.cs b/samples/core/Miscellaneous/NullableReferenceTypes/Program.cs index c75e1242d3..450f3589bd 100644 --- a/samples/core/Miscellaneous/NullableReferenceTypes/Program.cs +++ b/samples/core/Miscellaneous/NullableReferenceTypes/Program.cs @@ -45,10 +45,6 @@ private static void Main(string[] args) // Console.WriteLine(order.ShippingAddress.City); // The following would be a programming bug: we forgot to include Product above; will throw NullReferenceException. It would throw NullReferenceException. // Console.WriteLine(order.Product.Name); - - #region Navigating - Console.WriteLine(order.OptionalInfo!.ExtraAdditionalInfo!.SomeExtraAdditionalInfo); - #endregion } } }