Skip to content

Commit

Permalink
Add regression test for cycle detection using lazy-loading proxies
Browse files Browse the repository at this point in the history
Fixes #22391
  • Loading branch information
ajcvickers committed Sep 1, 2021
1 parent f31406b commit 3a58ff4
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ protected ProxyGraphUpdatesInMemoryTestBase(TFixture fixture)
{
}

public override void Attempting_to_save_two_entity_cycle_with_lazy_loading_throws()
{
}

// #11552
public override void Save_required_one_to_one_changed_by_reference(ChangeMechanism changeMechanism)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,20 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
});

modelBuilder.Entity<SharedFkDependant>();

modelBuilder.Entity<Person>(p =>
{
p.HasKey(tp => tp.Id);
});

modelBuilder.Entity<Car>(c =>
{
c.HasKey(tc => tc.Id);
c.HasOne(tc => tc.Owner)
.WithOne(tp => tp.Vehicle)
.HasForeignKey<Car>("fk_PersonId")
.IsRequired();
});
}

protected virtual object CreateFullGraph(DbContext context)
Expand Down Expand Up @@ -1880,6 +1894,18 @@ public class SharedFkDependant
public virtual SharedFkParent Parent { get; set; }
}

public class Car
{
public virtual Guid Id { get; set; }
public virtual Person Owner { get; set; }
}

public class Person
{
public virtual Guid Id { get; set; }
public virtual Car Vehicle { get; set; }
}

protected DbContext CreateContext()
=> Fixture.CreateContext();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Xunit;

// ReSharper disable InconsistentNaming
Expand All @@ -13,6 +15,42 @@ namespace Microsoft.EntityFrameworkCore
public abstract partial class ProxyGraphUpdatesTestBase<TFixture>
where TFixture : ProxyGraphUpdatesTestBase<TFixture>.ProxyGraphUpdatesFixtureBase, new()
{
[ConditionalFact]
public virtual void Attempting_to_save_two_entity_cycle_with_lazy_loading_throws()
{
ExecuteWithStrategyInTransaction(
context =>
{
context.AddRange(
context.CreateProxy<Car>(
car =>
{
car.Owner = context.CreateProxy<Person>();
car.Id = Guid.NewGuid();
}),
context.CreateProxy<Car>(
car =>
{
car.Owner = context.CreateProxy<Person>();
car.Id = Guid.NewGuid();
}));
context.SaveChanges();
},
context =>
{
var cars = context.Set<Car>().ToList();
(cars[1].Owner, cars[0].Owner) = (cars[0].Owner, cars[1].Owner);
cars[0].Owner.Vehicle = cars[0];
cars[1].Owner.Vehicle = cars[1];
var message = Assert.Throws<InvalidOperationException>(() => context.SaveChanges()).Message;
Assert.StartsWith(CoreStrings.CircularDependency("").Substring(0, 30), message);
});
}

[ConditionalFact]
public virtual void Avoid_nulling_shared_FK_property_when_deleting()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public ChangeTracking(ProxyGraphUpdatesWithChangeTrackingSqlServerFixture fixtur
{
}

// Needs lazy-loading
public override void Attempting_to_save_two_entity_cycle_with_lazy_loading_throws()
{
}

protected override bool DoesLazyLoading
=> false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public ChangeTracking(ProxyGraphUpdatesWithChangeTrackingSqliteFixture fixture)
{
}

// Needs lazy-loading
public override void Attempting_to_save_two_entity_cycle_with_lazy_loading_throws()
{
}

protected override bool DoesLazyLoading
=> false;

Expand Down

0 comments on commit 3a58ff4

Please sign in to comment.