Skip to content

Commit 4df8e7b

Browse files
committed
Throw when attempting to lazy-load after no-tracking query
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
1 parent 168145c commit 4df8e7b

File tree

3 files changed

+261
-105
lines changed

3 files changed

+261
-105
lines changed

src/EFCore.Specification.Tests/LazyLoadProxyTestBase.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,48 @@ public virtual void Lazy_load_reference_to_dependent_for_detached_is_no_op()
13801380
Assert.Null(parent.Single);
13811381
}
13821382
}
1383+
1384+
[Fact]
1385+
public virtual void Lazy_load_collection_for_no_tracking_throws()
1386+
{
1387+
using (var context = CreateContext(lazyLoadingEnabled: true))
1388+
{
1389+
var parent = context.Set<Parent>().AsNoTracking().Single();
1390+
1391+
Assert.Equal(
1392+
CoreStrings.CannotLoadDetached(nameof(Parent.Children), nameof(Parent)),
1393+
Assert.Throws<InvalidOperationException>(
1394+
() => parent.Children).Message);
1395+
}
1396+
}
1397+
1398+
[Fact]
1399+
public virtual void Lazy_load_reference_to_principal_for_no_tracking_throws()
1400+
{
1401+
using (var context = CreateContext(lazyLoadingEnabled: true))
1402+
{
1403+
var child = context.Set<Child>().AsNoTracking().Single(e => e.Id == 12);
1404+
1405+
Assert.Equal(
1406+
CoreStrings.CannotLoadDetached(nameof(Child.Parent), nameof(Child)),
1407+
Assert.Throws<InvalidOperationException>(
1408+
() => child.Parent).Message);
1409+
}
1410+
}
1411+
1412+
[Fact]
1413+
public virtual void Lazy_load_reference_to_dependent_for_no_tracking_throws()
1414+
{
1415+
using (var context = CreateContext(lazyLoadingEnabled: true))
1416+
{
1417+
var parent = context.Set<Parent>().AsNoTracking().Single();
1418+
1419+
Assert.Equal(
1420+
CoreStrings.CannotLoadDetached(nameof(Parent.Single), nameof(Parent)),
1421+
Assert.Throws<InvalidOperationException>(
1422+
() => parent.Single).Message);
1423+
}
1424+
}
13831425

13841426
[Theory]
13851427
[InlineData(EntityState.Unchanged, true)]

0 commit comments

Comments
 (0)