Skip to content

Commit

Permalink
Fix #8843 - Automatic eager loading of owned types.
Browse files Browse the repository at this point in the history
- Adds INavigation.IsEager - navs can now be marked as eager by-default (core metadata only).
- Adds a core convention that sets IsEager=true for principal-to-dependent navs when FK.IsOwnership=true.
- Updates the query compiler to auto-synthesize Include trees for owned navs on result QSREs.
- Adds a new query test suite for testing queries involving ownership.
  • Loading branch information
anpete committed Jun 20, 2017
1 parent 3fbc947 commit 80e5dee
Show file tree
Hide file tree
Showing 24 changed files with 556 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,12 @@ public virtual void Simple_owned_level1()
AssertQuery<Level1>(l1s => l1s.Include(l1 => l1.OneToOne_Required_PK), elementSorter: e => e.Id);
}

[ConditionalFact]
public virtual void Simple_owned_level1_convention()
{
AssertQuery<Level1>(l1s => l1s, elementSorter: e => e.Id);
}

[ConditionalFact]
public virtual void Simple_owned_level1_level2()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.EntityFrameworkCore
namespace Microsoft.EntityFrameworkCore.Query
{
public abstract class OneToOneQueryFixtureBase
{
Expand Down
98 changes: 98 additions & 0 deletions src/EFCore.Specification.Tests/Query/OwnedQueryFixtureBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.EntityFrameworkCore.Query
{
public abstract class OwnedQueryFixtureBase
{
protected virtual void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<OwnedPerson>().OwnsOne(p => p.PersonAddress).OwnsOne(a => a.Country);
modelBuilder.Entity<Branch>().OwnsOne(p => p.BranchAddress).OwnsOne(a => a.Country);
modelBuilder.Entity<LeafA>().OwnsOne(p => p.LeafAAddress).OwnsOne(a => a.Country);
modelBuilder.Entity<LeafB>().OwnsOne(p => p.LeafBAddress).OwnsOne(a => a.Country);
}

protected static void AddTestData(DbContext context)
{
context.Set<OwnedPerson>().AddRange(
new OwnedPerson
{
PersonAddress = new OwnedAddress
{
Country = new OwnedCountry { Name = "USA" }
}
},
new Branch
{
PersonAddress = new OwnedAddress
{
Country = new OwnedCountry { Name = "USA" }
},
BranchAddress = new OwnedAddress
{
Country = new OwnedCountry { Name = "Canada" }
}
},
new LeafA
{
PersonAddress = new OwnedAddress
{
Country = new OwnedCountry { Name = "USA" }
},
BranchAddress = new OwnedAddress
{
Country = new OwnedCountry { Name = "Canada" }
},
LeafAAddress = new OwnedAddress
{
Country = new OwnedCountry { Name = "Mexico" }
}
},
new LeafB
{
PersonAddress = new OwnedAddress
{
Country = new OwnedCountry { Name = "USA" }
},
LeafBAddress = new OwnedAddress
{
Country = new OwnedCountry { Name = "Panama" }
}
});

context.SaveChanges();
}
}

public class OwnedAddress
{
public OwnedCountry Country { get; set; }
}

public class OwnedCountry
{
public string Name { get; set; }
}

public class OwnedPerson
{
public int Id { get; set; }
public OwnedAddress PersonAddress { get; set; }
}

public class Branch : OwnedPerson
{
public OwnedAddress BranchAddress { get; set; }
}

public class LeafA : Branch
{
public OwnedAddress LeafAAddress { get; set; }
}

public class LeafB : OwnedPerson
{
public OwnedAddress LeafBAddress { get; set; }
}
}
57 changes: 57 additions & 0 deletions src/EFCore.Specification.Tests/Query/OwnedQueryTestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Linq;
using Xunit;
// ReSharper disable InconsistentNaming

namespace Microsoft.EntityFrameworkCore.Query
{
public abstract class OwnedQueryTestBase
{
[Fact]
public virtual void Query_for_base_type_loads_all_owned_navs()
{
using (var context = CreateContext())
{
var people = context.Set<OwnedPerson>().ToList();

Assert.Equal(4, people.Count);
Assert.True(people.All(p => p.PersonAddress != null));
Assert.True(people.OfType<Branch>().All(b => b.BranchAddress != null));
Assert.True(people.OfType<LeafA>().All(a => a.LeafAAddress != null));
Assert.True(people.OfType<LeafB>().All(b => b.LeafBAddress != null));
}
}

[Fact]
public virtual void Query_for_branch_type_loads_all_owned_navs()
{
using (var context = CreateContext())
{
var people = context.Set<Branch>().ToList();

Assert.Equal(2, people.Count);
Assert.True(people.All(p => p.PersonAddress != null));
Assert.True(people.All(b => b.BranchAddress != null));
Assert.True(people.OfType<LeafA>().All(a => a.LeafAAddress != null));
}
}

[Fact]
public virtual void Query_for_leaf_type_loads_all_owned_navs()
{
using (var context = CreateContext())
{
var people = context.Set<LeafA>().ToList();

Assert.Equal(1, people.Count);
Assert.True(people.All(p => p.PersonAddress != null));
Assert.True(people.All(b => b.BranchAddress != null));
Assert.True(people.All(a => a.LeafAAddress != null));
}
}

protected abstract DbContext CreateContext();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public virtual ConventionSet CreateConventionSet()
conventionSet.ForeignKeyUniquenessChangedConventions.Add(foreignKeyPropertyDiscoveryConvention);
conventionSet.ForeignKeyUniquenessChangedConventions.Add(foreignKeyIndexConvention);

conventionSet.ForeignKeyOwnershipChangedConventions.Add(new NavigationEagerLoadingConvention());

conventionSet.ModelBuiltConventions.Add(new ModelCleanupConvention());
conventionSet.ModelBuiltConventions.Add(keyAttributeConvention);
conventionSet.ModelBuiltConventions.Add(new IgnoredMembersValidationConvention());
Expand Down Expand Up @@ -144,7 +146,7 @@ public virtual ConventionSet CreateConventionSet()
conventionSet.PropertyFieldChangedConventions.Add(maxLengthAttributeConvention);
conventionSet.PropertyFieldChangedConventions.Add(stringLengthAttributeConvention);
conventionSet.PropertyFieldChangedConventions.Add(timestampAttributeConvention);

return conventionSet;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal
{
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public class NavigationEagerLoadingConvention : IForeignKeyOwnershipChangedConvention
{
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual InternalRelationshipBuilder Apply(InternalRelationshipBuilder relationshipBuilder)
{
relationshipBuilder.Metadata.PrincipalToDependent.IsEager = relationshipBuilder.Metadata.IsOwnership;

return relationshipBuilder;
}
}
}
5 changes: 5 additions & 0 deletions src/EFCore/Metadata/IMutableNavigation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@ public interface IMutableNavigation : INavigation, IMutablePropertyBase
/// Gets the foreign key that defines the relationship this navigation property will navigate.
/// </summary>
new IMutableForeignKey ForeignKey { get; }

/// <summary>
/// Determines whether this navigation should be eager loaded by default.
/// </summary>
new bool IsEager { get; set; }
}
}
5 changes: 5 additions & 0 deletions src/EFCore/Metadata/INavigation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@ public interface INavigation : IPropertyBase
/// Gets the foreign key that defines the relationship this navigation property will navigate.
/// </summary>
IForeignKey ForeignKey { get; }

/// <summary>
/// Determines whether this navigation should be eager loaded by default.
/// </summary>
bool IsEager { get; }
}
}
19 changes: 8 additions & 11 deletions src/EFCore/Metadata/Internal/ForeignKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,9 @@ public virtual ForeignKey SetIsUnique(bool unique, ConfigurationSource configura
_isUnique = unique;
UpdateIsUniqueConfigurationSource(configurationSource);

if (isChanging)
{
return DeclaringEntityType.Model.ConventionDispatcher.OnForeignKeyUniquenessChanged(Builder)?.Metadata;
}

return this;
return isChanging
? DeclaringEntityType.Model.ConventionDispatcher.OnForeignKeyUniquenessChanged(Builder)?.Metadata
: this;
}

private static bool DefaultIsUnique => false;
Expand All @@ -394,7 +391,7 @@ public virtual void UpdateIsUniqueConfigurationSource(ConfigurationSource config
public virtual bool IsRequired
{
get { return !Properties.Any(p => p.IsNullable); }
set { SetIsRequired(value, ConfigurationSource.Explicit); }
set => SetIsRequired(value, ConfigurationSource.Explicit);
}

/// <summary>
Expand Down Expand Up @@ -454,8 +451,8 @@ public virtual void UpdateIsRequiredConfigurationSource(ConfigurationSource conf
/// </summary>
public virtual DeleteBehavior DeleteBehavior
{
get { return _deleteBehavior ?? DefaultDeleteBehavior; }
set { SetDeleteBehavior(value, ConfigurationSource.Explicit); }
get => _deleteBehavior ?? DefaultDeleteBehavior;
set => SetDeleteBehavior(value, ConfigurationSource.Explicit);
}

/// <summary>
Expand Down Expand Up @@ -489,8 +486,8 @@ public virtual void UpdateDeleteBehaviorConfigurationSource(ConfigurationSource
/// </summary>
public virtual bool IsOwnership
{
get { return _isOwnership ?? DefaultIsOwnership; }
set { SetIsOwnership(value, ConfigurationSource.Explicit); }
get => _isOwnership ?? DefaultIsOwnership;
set => SetIsOwnership(value, ConfigurationSource.Explicit);
}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions src/EFCore/Metadata/Internal/InternalEntityTypeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1729,12 +1729,15 @@ private InternalRelationshipBuilder Owns(
var existingNavigation = Metadata
.FindNavigationsInHierarchy(navigation.Name)
.SingleOrDefault(n => n.GetTargetType().Name == targetEntityType.Name && n.GetTargetType().HasDefiningNavigation());

var builder = existingNavigation?.ForeignKey.Builder;

if (builder != null)
{
builder = builder.RelatedEntityTypes(Metadata, existingNavigation.GetTargetType(), configurationSource);
builder = builder?.IsOwnership(true, configurationSource);
builder = builder?.Navigations(inverse, navigation, configurationSource);

return builder == null ? null : batch.Run(builder);
}

Expand Down
6 changes: 6 additions & 0 deletions src/EFCore/Metadata/Internal/Navigation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public Navigation(
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual ForeignKey ForeignKey { get; }

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual bool IsEager { get; set; }

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

0 comments on commit 80e5dee

Please sign in to comment.