Skip to content

Commit

Permalink
Throw when attempting to lazy-load after no-tracking query
Browse files Browse the repository at this point in the history
Part of #10042, #10509, #3797

It would be good to make this work in a future release, but this involves running a no-tracking query with fixup where the root entity is already materialized. For now, this will throw so we can make it work later without it being a breaking change.

Lazy-loading behaviors for non-tracked entities:
* Proxies:
  * No-op if entity was explicitly detached
  * Throw if it was queried as no-tracking
* Lazy-loading entities with loader service property:
  * No-op if entity was explicitly detached
  * Throw if it was queried as no-tracking
* Lazy-loading entities without service property:
  * Throw even if entity was explicitly detached, but entity can set loader to null, or a service property can be defined
  * Throw if it was queried as no-tracking
  • Loading branch information
ajcvickers committed Feb 5, 2018
1 parent 168145c commit 4df8e7b
Show file tree
Hide file tree
Showing 3 changed files with 261 additions and 105 deletions.
42 changes: 42 additions & 0 deletions src/EFCore.Specification.Tests/LazyLoadProxyTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,48 @@ public virtual void Lazy_load_reference_to_dependent_for_detached_is_no_op()
Assert.Null(parent.Single);
}
}

[Fact]
public virtual void Lazy_load_collection_for_no_tracking_throws()
{
using (var context = CreateContext(lazyLoadingEnabled: true))
{
var parent = context.Set<Parent>().AsNoTracking().Single();

Assert.Equal(
CoreStrings.CannotLoadDetached(nameof(Parent.Children), nameof(Parent)),
Assert.Throws<InvalidOperationException>(
() => parent.Children).Message);
}
}

[Fact]
public virtual void Lazy_load_reference_to_principal_for_no_tracking_throws()
{
using (var context = CreateContext(lazyLoadingEnabled: true))
{
var child = context.Set<Child>().AsNoTracking().Single(e => e.Id == 12);

Assert.Equal(
CoreStrings.CannotLoadDetached(nameof(Child.Parent), nameof(Child)),
Assert.Throws<InvalidOperationException>(
() => child.Parent).Message);
}
}

[Fact]
public virtual void Lazy_load_reference_to_dependent_for_no_tracking_throws()
{
using (var context = CreateContext(lazyLoadingEnabled: true))
{
var parent = context.Set<Parent>().AsNoTracking().Single();

Assert.Equal(
CoreStrings.CannotLoadDetached(nameof(Parent.Single), nameof(Parent)),
Assert.Throws<InvalidOperationException>(
() => parent.Single).Message);
}
}

[Theory]
[InlineData(EntityState.Unchanged, true)]
Expand Down
Loading

0 comments on commit 4df8e7b

Please sign in to comment.