Skip to content

Commit

Permalink
Don't throw when attempting to load from a newly Added entity with un…
Browse files Browse the repository at this point in the history
…known FK values (#32343)
  • Loading branch information
ajcvickers committed Nov 20, 2023
1 parent 6980d92 commit 11d0310
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/EFCore/Internal/EntityFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
12 changes: 8 additions & 4 deletions test/EFCore.Specification.Tests/LazyLoadTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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"),
Expand Down

0 comments on commit 11d0310

Please sign in to comment.