-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #8843 - Automatic eager loading of owned types.
- 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
Showing
23 changed files
with
555 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ication.Tests/OneToOneQueryFixtureBase.cs → ...n.Tests/Query/OneToOneQueryFixtureBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/EFCore.Specification.Tests/Query/OwnedQueryFixtureBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
57
src/EFCore.Specification.Tests/Query/OwnedQueryTestBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/EFCore/Metadata/Conventions/Internal/NavigationEagerLoadingConvention.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.