-
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.
Conasistent fixup from query when reference navigation explicitly set
Fixes #18007 Previously it was possible for the existing instance to be replaced when a query brings back a new instance, but only for the first time the new instance is returned. This change prevents the new instance from being used in all cases, which is the agreed behavior here, and also now matches what EF6 does consistently.
- Loading branch information
1 parent
7641cc3
commit 1c6e891
Showing
6 changed files
with
209 additions
and
3 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
22 changes: 22 additions & 0 deletions
22
test/EFCore.Cosmos.FunctionalTests/OverzealousInitializationCosmosTest.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,22 @@ | ||
// 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.Cosmos.TestUtilities; | ||
using Microsoft.EntityFrameworkCore.TestUtilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Cosmos | ||
{ | ||
public class OverzealousInitializationCosmosTest | ||
: OverzealousInitializationTestBase<OverzealousInitializationCosmosTest.OverzealousInitializationCosmosFixture> | ||
{ | ||
public OverzealousInitializationCosmosTest(OverzealousInitializationCosmosFixture fixture) | ||
: base(fixture) | ||
{ | ||
} | ||
|
||
public class OverzealousInitializationCosmosFixture : OverzealousInitializationFixtureBase | ||
{ | ||
protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance; | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
test/EFCore.InMemory.FunctionalTests/OverzealousInitializationInMemoryTest.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,21 @@ | ||
// 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.TestUtilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore | ||
{ | ||
public class OverzealousInitializationInMemoryTest | ||
: OverzealousInitializationTestBase<OverzealousInitializationInMemoryTest.OverzealousInitializationInMemoryFixture> | ||
{ | ||
public OverzealousInitializationInMemoryTest(OverzealousInitializationInMemoryFixture fixture) | ||
: base(fixture) | ||
{ | ||
} | ||
|
||
public class OverzealousInitializationInMemoryFixture : OverzealousInitializationFixtureBase | ||
{ | ||
protected override ITestStoreFactory TestStoreFactory => InMemoryTestStoreFactory.Instance; | ||
} | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
test/EFCore.Specification.Tests/OverzealousInitializationTestBase.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,118 @@ | ||
// 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; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
using System.Linq; | ||
using Microsoft.EntityFrameworkCore.TestUtilities; | ||
using Xunit; | ||
|
||
namespace Microsoft.EntityFrameworkCore | ||
{ | ||
public abstract class OverzealousInitializationTestBase<TFixture> : IClassFixture<TFixture> | ||
where TFixture : OverzealousInitializationTestBase<TFixture>.OverzealousInitializationFixtureBase, new() | ||
{ | ||
protected OverzealousInitializationTestBase(TFixture fixture) => Fixture = fixture; | ||
|
||
[ConditionalFact] | ||
public void Fixup_does_not_ignore_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) | ||
.ToList(); | ||
|
||
foreach (var album in albums) | ||
{ | ||
Assert.Equal(0, album.Artist.Id); | ||
Assert.Null(album.Artist.Name); | ||
} | ||
} | ||
|
||
protected class Album | ||
{ | ||
[DatabaseGenerated(DatabaseGeneratedOption.None)] | ||
public int Id { get; set; } | ||
public int ArtistId { get; set; } | ||
|
||
public virtual Artist Artist { get; set; } | ||
public virtual IList<Track> Tracks { get; set; } | ||
|
||
public Album() | ||
{ | ||
Artist = new Artist(); | ||
Tracks = new List<Track>(); | ||
} | ||
} | ||
|
||
protected class Artist | ||
{ | ||
[DatabaseGenerated(DatabaseGeneratedOption.None)] | ||
public int Id { get; set; } | ||
public string Name { get; set; } | ||
} | ||
|
||
public class Track | ||
{ | ||
[DatabaseGenerated(DatabaseGeneratedOption.None)] | ||
public int Id { get; set; } | ||
public int AlbumId { get; set; } | ||
} | ||
|
||
public class AlbumViewerContext : PoolableDbContext | ||
{ | ||
public AlbumViewerContext(DbContextOptions<AlbumViewerContext> options) | ||
: base(options) | ||
{ | ||
} | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.Entity<Album>(); | ||
modelBuilder.Entity<Artist>(); | ||
modelBuilder.Entity<Track>(); | ||
} | ||
} | ||
|
||
protected TFixture Fixture { get; } | ||
|
||
protected AlbumViewerContext CreateContext() => Fixture.CreateContext(); | ||
|
||
public abstract class OverzealousInitializationFixtureBase : SharedStoreFixtureBase<AlbumViewerContext> | ||
{ | ||
public virtual IDisposable BeginTransaction(DbContext context) => context.Database.BeginTransaction(); | ||
|
||
protected override string StoreName { get; } = "OverzealousInitialization"; | ||
|
||
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], | ||
Tracks = new List<Track> | ||
{ | ||
new Track { Id = i * 2 }, | ||
new Track { Id = i * 2 + 1} | ||
} | ||
}); | ||
} | ||
|
||
context.SaveChanges(); | ||
} | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
test/EFCore.SqlServer.FunctionalTests/OverzealousInitializationSqlServerTest.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,21 @@ | ||
// 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.TestUtilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore | ||
{ | ||
public class OverzealousInitializationSqlServerTest | ||
: OverzealousInitializationTestBase<OverzealousInitializationSqlServerTest.OverzealousInitializationSqlServerFixture> | ||
{ | ||
public OverzealousInitializationSqlServerTest(OverzealousInitializationSqlServerFixture fixture) | ||
: base(fixture) | ||
{ | ||
} | ||
|
||
public class OverzealousInitializationSqlServerFixture : OverzealousInitializationFixtureBase | ||
{ | ||
protected override ITestStoreFactory TestStoreFactory => SqlServerTestStoreFactory.Instance; | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
test/EFCore.Sqlite.FunctionalTests/OverzealousInitializationSqliteTest.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,21 @@ | ||
// 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.TestUtilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore | ||
{ | ||
public class OverzealousInitializationSqliteTest | ||
: OverzealousInitializationTestBase<OverzealousInitializationSqliteTest.OverzealousInitializationSqliteFixture> | ||
{ | ||
public OverzealousInitializationSqliteTest(OverzealousInitializationSqliteFixture fixture) | ||
: base(fixture) | ||
{ | ||
} | ||
|
||
public class OverzealousInitializationSqliteFixture : OverzealousInitializationFixtureBase | ||
{ | ||
protected override ITestStoreFactory TestStoreFactory => SqliteTestStoreFactory.Instance; | ||
} | ||
} | ||
} |