Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build QueryRoot correctly for entity entries #18434

Merged
merged 1 commit into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/EFCore/Internal/EntityFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,18 +321,19 @@ private static Expression<Func<object, bool>> BuildObjectLambda(IReadOnlyList<IP

private IQueryable BuildQueryRoot(IEntityType entityType)
{
var definingEntityType = entityType.DefiningEntityType;
return definingEntityType == null
? (IQueryable)_setCache.GetOrAddSet(_setSource, entityType.ClrType)
: BuildQueryRoot(definingEntityType, entityType);
return entityType.DefiningEntityType is IEntityType definingEntityType
? BuildQueryRoot(definingEntityType, entityType, entityType.DefiningNavigationName)
: entityType.FindOwnership() is IForeignKey ownership
? BuildQueryRoot(ownership.PrincipalEntityType, entityType, ownership.PrincipalToDependent.Name)
: (IQueryable)_setCache.GetOrAddSet(_setSource, entityType.ClrType);
}

private IQueryable BuildQueryRoot(IEntityType definingEntityType, IEntityType entityType)
private IQueryable BuildQueryRoot(IEntityType ownerOrDefiningEntityType, IEntityType entityType, string navigationName)
{
var queryRoot = BuildQueryRoot(definingEntityType);
var queryRoot = BuildQueryRoot(ownerOrDefiningEntityType);

return (IQueryable)_selectMethod.MakeGenericMethod(definingEntityType.ClrType, entityType.ClrType)
.Invoke(null, new object[] { queryRoot, entityType.DefiningNavigationName });
return (IQueryable)_selectMethod.MakeGenericMethod(ownerOrDefiningEntityType.ClrType, entityType.ClrType)
.Invoke(null, new object[] { queryRoot, navigationName });
}

private static readonly MethodInfo _selectMethod
Expand Down
80 changes: 79 additions & 1 deletion test/EFCore.Specification.Tests/OptimisticConcurrencyTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ public virtual Task
[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public virtual async Task Calling_Reload_on_an__Added_entity_that_is_not_in_database_is_no_op(bool async)
public virtual async Task Calling_Reload_on_an_Added_entity_that_is_not_in_database_is_no_op(bool async)
{
using (var c = CreateF1Context())
{
Expand Down Expand Up @@ -598,6 +598,84 @@ await c.Database.CreateExecutionStrategy().ExecuteAsync(
}
}

[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public virtual async Task Calling_GetDatabaseValues_on_owned_entity_works(bool async)
{
using (var c = CreateF1Context())
{
await c.Database.CreateExecutionStrategy().ExecuteAsync(
c, async context =>
{
using (var transaction = context.Database.BeginTransaction())
{
var titleSponsor = context.Set<TitleSponsor>().Single(t => t.Name == "Vodafone");

var ownerEntry = context.Entry(titleSponsor);
var ownedEntry = ownerEntry.Reference(e => e.Details).TargetEntry;

using (var innerContext = CreateF1Context())
{
UseTransaction(innerContext.Database, transaction);

var innerTitleSponsor = innerContext.Set<TitleSponsor>().Single(t => t.Name == "Vodafone");
innerTitleSponsor.Details.Days = 5;

await innerContext.SaveChangesAsync();

var databaseValues = async
? await ownedEntry.GetDatabaseValuesAsync()
: ownedEntry.GetDatabaseValues();
Assert.Equal(5, databaseValues.GetValue<int>("Days"));
}
}
});
}
}

[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public virtual async Task Calling_Reload_on_owned_entity_works(bool async)
{
using (var c = CreateF1Context())
{
await c.Database.CreateExecutionStrategy().ExecuteAsync(
c, async context =>
{
using (var transaction = context.Database.BeginTransaction())
{
var titleSponsor = context.Set<TitleSponsor>().Single(t => t.Name == "Vodafone");

var ownerEntry = context.Entry(titleSponsor);
var ownedEntry = ownerEntry.Reference(e => e.Details).TargetEntry;

using (var innerContext = CreateF1Context())
{
UseTransaction(innerContext.Database, transaction);

var innerTitleSponsor = innerContext.Set<TitleSponsor>().Single(t => t.Name == "Vodafone");
innerTitleSponsor.Details.Days = 5;

await innerContext.SaveChangesAsync();

if (async)
{
await ownedEntry.ReloadAsync();
}
else
{
ownedEntry.Reload();
}

Assert.Equal(5, ownedEntry.Property(e => e.Days).CurrentValue);
}
}
});
}
}

#endregion

#region Helpers
Expand Down