From 608ff81038afa70507bbb9c52df50083ce80240a Mon Sep 17 00:00:00 2001 From: Skyrunner-Dev-ops Date: Thu, 8 Oct 2026 00:10:31 +0530 Subject: [PATCH 1/2] Clarify that Include requires navigation property access (#33018) Changed InvalidIncludeExpression error message from 'property access' to 'navigation property access' to clarify that Include only works with navigation properties, not regular/complex type properties. Added Include_complex_property test in ComplexTypeQueryTestBase to verify the improved error message when attempting to Include a complex type property. --- src/EFCore/Properties/CoreStrings.Designer.cs | 2 +- src/EFCore/Properties/CoreStrings.resx | 2 +- .../Query/ComplexTypeQueryTestBase.cs | 8 ++++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/EFCore/Properties/CoreStrings.Designer.cs b/src/EFCore/Properties/CoreStrings.Designer.cs index a481bcb514f..9623ce1884c 100644 --- a/src/EFCore/Properties/CoreStrings.Designer.cs +++ b/src/EFCore/Properties/CoreStrings.Designer.cs @@ -1862,7 +1862,7 @@ public static string InvalidEnumValue(object? value, object? argumentName, objec value, argumentName, enumType); /// - /// The expression '{expression}' is invalid inside an 'Include' operation, since it does not represent a property access: 't => t.MyProperty'. To target navigations declared on derived types, use casting ('t => ((Derived)t).MyProperty') or the 'as' operator ('t => (t as Derived).MyProperty'). Collection navigation access can be filtered by composing Where, OrderBy(Descending), ThenBy(Descending), Skip or Take operations. For more information on including related data, see https://go.microsoft.com/fwlink/?LinkID=746393. + /// The expression '{expression}' is invalid inside an 'Include' operation, since it does not represent a navigation property access: 't => t.MyProperty'. To target navigations declared on derived types, use casting ('t => ((Derived)t).MyProperty') or the 'as' operator ('t => (t as Derived).MyProperty'). Collection navigation access can be filtered by composing Where, OrderBy(Descending), ThenBy(Descending), Skip or Take operations. For more information on including related data, see https://go.microsoft.com/fwlink/?LinkID=746393. /// public static string InvalidIncludeExpression(object? expression) => string.Format( diff --git a/src/EFCore/Properties/CoreStrings.resx b/src/EFCore/Properties/CoreStrings.resx index e84e4f08512..e1d654305bb 100644 --- a/src/EFCore/Properties/CoreStrings.resx +++ b/src/EFCore/Properties/CoreStrings.resx @@ -821,7 +821,7 @@ The value '{value}' provided for argument '{argumentName}' must be a valid value of enum type '{enumType}'. - The expression '{expression}' is invalid inside an 'Include' operation, since it does not represent a property access: 't => t.MyProperty'. To target navigations declared on derived types, use casting ('t => ((Derived)t).MyProperty') or the 'as' operator ('t => (t as Derived).MyProperty'). Collection navigation access can be filtered by composing Where, OrderBy(Descending), ThenBy(Descending), Skip or Take operations. For more information on including related data, see https://go.microsoft.com/fwlink/?LinkID=746393. + The expression '{expression}' is invalid inside an 'Include' operation, since it does not represent a navigation property access: 't => t.MyProperty'. To target navigations declared on derived types, use casting ('t => ((Derived)t).MyProperty') or the 'as' operator ('t => (t as Derived).MyProperty'). Collection navigation access can be filtered by composing Where, OrderBy(Descending), ThenBy(Descending), Skip or Take operations. For more information on including related data, see https://go.microsoft.com/fwlink/?LinkID=746393. Unable to track an entity of type '{entityType}' because its primary key property '{keyProperty}' is null. diff --git a/test/EFCore.Specification.Tests/Query/ComplexTypeQueryTestBase.cs b/test/EFCore.Specification.Tests/Query/ComplexTypeQueryTestBase.cs index e00ffb36177..37da1b51071 100644 --- a/test/EFCore.Specification.Tests/Query/ComplexTypeQueryTestBase.cs +++ b/test/EFCore.Specification.Tests/Query/ComplexTypeQueryTestBase.cs @@ -110,6 +110,14 @@ public virtual Task Select_complex_type_Distinct(bool async) async, ss => ss.Set().Select(c => c.ShippingAddress).Distinct()); + [Theory, MemberData(nameof(IsAsyncData))] + public virtual async Task Include_complex_property(bool async) + => Assert.Equal( + CoreStrings.InvalidIncludeExpression("c.ShippingAddress"), + (await Assert.ThrowsAsync(() => AssertQuery( + async, + ss => ss.Set().Include(c => c.ShippingAddress)))).Message); + [Theory, MemberData(nameof(IsAsyncData))] public virtual Task Complex_type_equals_complex_type(bool async) => AssertQuery( From 17944c3aa989b5a43ea26b825b8b86df9ef1cc89 Mon Sep 17 00:00:00 2001 From: Skyrunner-Dev-ops Date: Wed, 14 Oct 2026 00:44:44 +0530 Subject: [PATCH 2/2] Add provider test overrides for Include_complex_property --- .../Query/ComplexTypeQueryCosmosTest.cs | 7 +++++++ .../Query/ComplexTypeToJsonPropertyQueryCosmosTest.cs | 7 +++++++ .../Query/ComplexTypeQuerySqlServerTest.cs | 7 +++++++ .../Query/ComplexTypeQuerySqliteTest.cs | 7 +++++++ 4 files changed, 28 insertions(+) diff --git a/test/EFCore.Cosmos.FunctionalTests/Query/ComplexTypeQueryCosmosTest.cs b/test/EFCore.Cosmos.FunctionalTests/Query/ComplexTypeQueryCosmosTest.cs index 5e4d89b10f1..5c35154bc34 100644 --- a/test/EFCore.Cosmos.FunctionalTests/Query/ComplexTypeQueryCosmosTest.cs +++ b/test/EFCore.Cosmos.FunctionalTests/Query/ComplexTypeQueryCosmosTest.cs @@ -108,6 +108,13 @@ FROM root c """); }); + public override async Task Include_complex_property(bool async) + { + await base.Include_complex_property(async); + + AssertSql(); + } + public override Task Complex_type_equals_complex_type(bool async) => CosmosTestHelpers.Instance.NoSyncTest( async, async async => diff --git a/test/EFCore.Cosmos.FunctionalTests/Query/ComplexTypeToJsonPropertyQueryCosmosTest.cs b/test/EFCore.Cosmos.FunctionalTests/Query/ComplexTypeToJsonPropertyQueryCosmosTest.cs index 8076ea87bc0..864a2977bca 100644 --- a/test/EFCore.Cosmos.FunctionalTests/Query/ComplexTypeToJsonPropertyQueryCosmosTest.cs +++ b/test/EFCore.Cosmos.FunctionalTests/Query/ComplexTypeToJsonPropertyQueryCosmosTest.cs @@ -110,6 +110,13 @@ FROM root c """); }); + public override async Task Include_complex_property(bool async) + { + await base.Include_complex_property(async); + + AssertSql(); + } + public override Task Complex_type_equals_complex_type(bool async) => CosmosTestHelpers.Instance.NoSyncTest( async, async async => diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/ComplexTypeQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/ComplexTypeQuerySqlServerTest.cs index 68661059aa4..3ec74f40d48 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/ComplexTypeQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/ComplexTypeQuerySqlServerTest.cs @@ -204,6 +204,13 @@ FROM [Customer] AS [c] """); } + public override async Task Include_complex_property(bool async) + { + await base.Include_complex_property(async); + + AssertSql(); + } + public override async Task Complex_type_equals_complex_type(bool async) { await base.Complex_type_equals_complex_type(async); diff --git a/test/EFCore.Sqlite.FunctionalTests/Query/ComplexTypeQuerySqliteTest.cs b/test/EFCore.Sqlite.FunctionalTests/Query/ComplexTypeQuerySqliteTest.cs index 38510fc64ba..a300bef0716 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Query/ComplexTypeQuerySqliteTest.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Query/ComplexTypeQuerySqliteTest.cs @@ -203,6 +203,13 @@ public override async Task Select_complex_type_Distinct(bool async) """); } + public override async Task Include_complex_property(bool async) + { + await base.Include_complex_property(async); + + AssertSql(); + } + public override async Task Complex_type_equals_complex_type(bool async) { await base.Complex_type_equals_complex_type(async);