Skip to content

Commit

Permalink
Always do tracking query for Load
Browse files Browse the repository at this point in the history
Issue: If the ChangeTracking behaviour is set to NoTracking then calling Load on tracked entity won't track resuls of load query. Which results in load failing.
The fix is to issue AsTracking query so that results from load query are added to state manager and will be fixed up with tracked query

Currently calling Load on non tracked query does not work so this fix is safe in that regard.

Resolves #12209
  • Loading branch information
smitpatel committed Jun 1, 2018
1 parent d2847f2 commit 929a7e5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/EFCore.Specification.Tests/Query/AsTrackingTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,29 @@ on c.CustomerID equals o.CustomerID
}
}

[ConditionalFact]
public virtual void Load_works_when_using_AsTracking()
{
using (var context = CreateContext())
{
var order = context.Orders.Where(o => o.OrderID == 10248).AsTracking().Single();

Assert.NotNull(order);

context.Entry(order)
.Collection(o => o.OrderDetails)
.Load();

Assert.NotEmpty(order.OrderDetails);

context.Entry(order)
.Reference(o => o.Customer)
.Load();

Assert.NotNull(order.Customer);
}
}

protected NorthwindContext CreateContext()
{
var context = Fixture.CreateContext();
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore/Internal/EntityFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private IQueryable<object[]> GetDatabaseValuesQuery(InternalEntityEntry entry)
}

private IQueryable<TEntity> Query(INavigation navigation, object[] keyValues)
=> _queryRoot.Where(BuildLambda(GetLoadProperties(navigation), new ValueBuffer(keyValues)));
=> _queryRoot.Where(BuildLambda(GetLoadProperties(navigation), new ValueBuffer(keyValues))).AsTracking();

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
Expand Down

0 comments on commit 929a7e5

Please sign in to comment.