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

Overwrite pre-initialized reference navigations when querying #25449

Merged
merged 2 commits into from
Aug 11, 2021
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
13 changes: 6 additions & 7 deletions src/EFCore/ChangeTracking/Internal/NavigationFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ private void InitialFixup(
var navigation = foreignKey.DependentToPrincipal;
var existingPrincipal = navigation == null ? null : entry[navigation];
if (existingPrincipal == null
|| fromQuery
|| existingPrincipal == principalEntry.Entity)
{
// Set navigation to principal based on FK properties
Expand All @@ -724,7 +725,8 @@ private void InitialFixup(
var dependentEntry = (InternalEntityEntry?)dependents.FirstOrDefault();
if (dependentEntry != null)
{
if ((!foreignKey.IsOwnership
if (fromQuery
|| (!foreignKey.IsOwnership
|| (dependentEntry.EntityState != EntityState.Deleted
&& dependentEntry.EntityState != EntityState.Detached))
&& (foreignKey.PrincipalToDependent == null
Expand All @@ -741,12 +743,9 @@ private void InitialFixup(
{
foreach (InternalEntityEntry dependentEntry in dependents)
{
if ((!foreignKey.IsOwnership
|| (dependentEntry.EntityState != EntityState.Deleted
&& dependentEntry.EntityState != EntityState.Detached))
&& (!fromQuery
|| foreignKey.DependentToPrincipal == null
|| dependentEntry.GetCurrentValue(foreignKey.DependentToPrincipal) == null))
if (!foreignKey.IsOwnership
|| (dependentEntry.EntityState != EntityState.Deleted
&& dependentEntry.EntityState != EntityState.Detached))
{
// Add to collection on principal indicated by FK and set inverse navigation
AddToCollection(entry, foreignKey.PrincipalToDependent, dependentEntry, fromQuery);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public OverzealousInitializationCosmosTest(OverzealousInitializationCosmosFixtur
}

[ConditionalFact(Skip = "Issue #17246")]
public override void Fixup_does_not_ignore_eagerly_initialized_reference_navs()
public override void Fixup_ignores_eagerly_initialized_reference_navs()
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public override async Task Include_collection_dependent_already_tracked(bool asy
using var context = CreateContext();
var orders = context.Set<Order>().Where(o => o.CustomerID == "ALFKI").ToList();
Assert.Equal(6, context.ChangeTracker.Entries().Count());
Assert.True(orders.All(o => o.Customer == null));
Assert.True(orders.All(o => o.Customer.CustomerID == null));

var customer
= async
Expand All @@ -70,7 +70,7 @@ var customer
Assert.True(customer.Orders.All(e => ReferenceEquals(e.Customer, customer)));

Assert.Equal(6, context.ChangeTracker.Entries().Count());
Assert.True(orders.All(o => o.Customer == null));
Assert.True(orders.All(o => o.Customer.CustomerID == null));
}

public override async Task Include_collection_principal_already_tracked(bool async)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,33 @@ protected OverzealousInitializationTestBase(TFixture fixture)
=> Fixture = fixture;

[ConditionalFact]
public virtual void Fixup_does_not_ignore_eagerly_initialized_reference_navs()
public virtual void Fixup_ignores_eagerly_initialized_reference_navs()
{
using var context = CreateContext();

var albums = context.Set<Album>()
.Include(e => e.Tracks)
.Include(e => e.Artist)
.OrderBy(e => e.Artist)
.OrderBy(e => e.Id)
.ToList();

var i = 0;
foreach (var album in albums)
{
Assert.Equal(0, album.Artist.Id);
Assert.Null(album.Artist.Name);
var artist = _artists[i++ % 3];

Assert.Equal(artist.Id, album.Artist.Id);
Assert.Equal(artist.Name, album.Artist.Name);
}
}

private static readonly Artist[] _artists = new[]
{
new Artist { Id = 1, Name = "Freddie" },
new Artist { Id = 2, Name = "Kendrick" },
new Artist { Id = 3, Name = "Jarvis" }
};

protected class Album
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
Expand Down Expand Up @@ -96,20 +106,13 @@ public virtual IDisposable BeginTransaction(DbContext context)

protected override void Seed(AlbumViewerContext context)
{
var artists = new[]
{
new Artist { Id = 1, Name = "Freddie" },
new Artist { Id = 2, Name = "Kendrick" },
new Artist { Id = 3, Name = "Jarvis" }
};

for (var i = 1; i <= 10; i++)
{
context.Add(
new Album
{
Id = i,
Artist = artists[i % 3],
Artist = _artists[(i - 1) % 3],
Tracks = new List<Track> { new() { Id = i * 2 }, new() { Id = i * 2 + 1 } }
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public override async Task Include_collection_dependent_already_tracked(bool asy
using var context = CreateContext();
var orders = context.Set<Order>().Where(o => o.CustomerID == "ALFKI").ToList();
Assert.Equal(6, context.ChangeTracker.Entries().Count());
Assert.True(orders.All(o => o.Customer == null));
Assert.True(orders.All(o => o.Customer.CustomerID == null));

var customer
= async
Expand All @@ -166,7 +166,7 @@ var customer
Assert.True(customer.Orders.All(e => ReferenceEquals(e.Customer, customer)));

Assert.Equal(6, context.ChangeTracker.Entries().Count());
Assert.True(orders.All(o => o.Customer == null));
Assert.True(orders.All(o => o.Customer.CustomerID == null));
}

public override async Task Include_collection_principal_already_tracked(bool async)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Gear()

public MilitaryRank Rank { get; set; }

public virtual CogTag Tag { get; set; }
public virtual CogTag Tag { get; set; } = new(); // Initialized to test #23851

public virtual Squad Squad { get; set; }

Expand Down
Loading