diff --git a/src/EFCore/Internal/EntityFinder.cs b/src/EFCore/Internal/EntityFinder.cs index 2e06583056c..b8bf35c8cba 100644 --- a/src/EFCore/Internal/EntityFinder.cs +++ b/src/EFCore/Internal/EntityFinder.cs @@ -715,7 +715,7 @@ IQueryable IEntityFinder.Query(INavigation navigation, InternalEntityEntry entry for (var i = 0; i < values.Length; i++) { var property = properties[i]; - if (property.IsShadowProperty() && (detached || entry.IsUnknown(property))) + if (property.IsShadowProperty() && (detached || (entry.EntityState != EntityState.Added && entry.IsUnknown(property)))) { throw new InvalidOperationException( CoreStrings.CannotLoadDetachedShadow(navigation.Name, entry.EntityType.DisplayName())); diff --git a/test/EFCore.Specification.Tests/LazyLoadTestBase.cs b/test/EFCore.Specification.Tests/LazyLoadTestBase.cs index fc6e6e3af0b..0328a2675f3 100644 --- a/test/EFCore.Specification.Tests/LazyLoadTestBase.cs +++ b/test/EFCore.Specification.Tests/LazyLoadTestBase.cs @@ -2006,7 +2006,8 @@ public virtual void Lazy_load_many_to_one_reference_to_principal_shadow_fk( if (LazyLoadingEnabled) { - if (state == EntityState.Detached && queryTrackingBehavior == QueryTrackingBehavior.TrackAll) + if (state == EntityState.Detached && queryTrackingBehavior == QueryTrackingBehavior.TrackAll + || state == EntityState.Added && queryTrackingBehavior != QueryTrackingBehavior.TrackAll) { Assert.Null(child.Parent); // Explicitly detached } @@ -2094,7 +2095,8 @@ public virtual void Lazy_load_one_to_one_reference_to_principal_shadow_fk( if (LazyLoadingEnabled) { - if (state == EntityState.Detached && queryTrackingBehavior == QueryTrackingBehavior.TrackAll) + if (state == EntityState.Detached && queryTrackingBehavior == QueryTrackingBehavior.TrackAll + || state == EntityState.Added && queryTrackingBehavior != QueryTrackingBehavior.TrackAll) { Assert.Null(single.Parent); // Explicitly detached } @@ -2258,7 +2260,8 @@ public virtual void Lazy_load_many_to_one_reference_to_principal_null_FK_shadow_ { Assert.Null(child.Parent); // Explicitly detached } - else if (queryTrackingBehavior != QueryTrackingBehavior.TrackAll) + else if (queryTrackingBehavior != QueryTrackingBehavior.TrackAll + && state != EntityState.Added) { Assert.Equal( CoreStrings.CannotLoadDetachedShadow("Parent", "ChildShadowFk"), @@ -2327,7 +2330,8 @@ public virtual void Lazy_load_one_to_one_reference_to_principal_null_FK_shadow_f { Assert.Null(single.Parent); } - else if (queryTrackingBehavior != QueryTrackingBehavior.TrackAll) + else if (queryTrackingBehavior != QueryTrackingBehavior.TrackAll + && state != EntityState.Added) { Assert.Equal( CoreStrings.CannotLoadDetachedShadow("Parent", "SingleShadowFk"),