Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,11 @@ private void AddMatchingPredecessorEdge<T>(
}

AfterLoop:
_modificationCommandGraph.AddEdge(predecessor, command, new CommandDependency(foreignKey), requiresBatchingBoundary);
_modificationCommandGraph.AddEdge(
predecessor,
command,
new CommandDependency(foreignKey, Breakable: !foreignKey.IsConstrained && !requiresBatchingBoundary),
Comment thread
AndriySvyryd marked this conversation as resolved.
requiresBatchingBoundary);
}
}
}
Expand Down
72 changes: 67 additions & 5 deletions test/EFCore.Relational.Tests/Update/CommandBatchPreparerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,50 @@ public void Batch_command_throws_on_commands_with_circular_dependencies(bool sen
.Message);
}

[Fact]
public void Batch_command_breaks_cycle_for_unconstrained_foreign_key()
{
var model = CreateCyclicFKModel(unconstrained: true);
var configuration = CreateContextServices(model);
var stateManager = configuration.GetRequiredService<IStateManager>();

var fakeEntry = stateManager.GetOrCreateEntry(
new FakeEntity { RelatedId = 1 });
fakeEntry.SetEntityState(EntityState.Added);

var temporaryIdValue = fakeEntry.GetCurrentValue<int>(fakeEntry.EntityType.GetProperty(nameof(FakeEntity.Id)));
var relatedFakeEntry = stateManager.GetOrCreateEntry(
new RelatedFakeEntity { Id = 1, RelatedId = temporaryIdValue });
relatedFakeEntry.SetEntityState(EntityState.Added);

var batches = CreateBatches([relatedFakeEntry, fakeEntry], new UpdateAdapter(stateManager));

Assert.Collection(
batches,
b => Assert.Same(fakeEntry, b.ModificationCommands.Single().Entries.Single()),
b => Assert.Same(relatedFakeEntry, b.ModificationCommands.Single().Entries.Single()));
}

[Fact]
public void Batch_command_does_not_break_cycle_for_unconstrained_foreign_key_with_store_generated_principal_key()
{
var model = CreateCyclicFKModel(unconstrainedPrincipalGenerated: true);
var configuration = CreateContextServices(model);
var stateManager = configuration.GetRequiredService<IStateManager>();

var fakeEntry = stateManager.GetOrCreateEntry(
new FakeEntity { RelatedId = 1 });
fakeEntry.SetEntityState(EntityState.Added);

var temporaryIdValue = fakeEntry.GetCurrentValue<int>(fakeEntry.EntityType.GetProperty(nameof(FakeEntity.Id)));
var relatedFakeEntry = stateManager.GetOrCreateEntry(
new RelatedFakeEntity { Id = 1, RelatedId = temporaryIdValue });
relatedFakeEntry.SetEntityState(EntityState.Added);

Assert.Throws<InvalidOperationException>(
() => CreateBatches([fakeEntry, relatedFakeEntry], new UpdateAdapter(stateManager)));
}

[InlineData(true), InlineData(false), Theory]
public void Batch_command_throws_on_commands_with_circular_dependencies_including_indexes(bool sensitiveLogging)
{
Expand Down Expand Up @@ -1083,7 +1127,7 @@ private static IModel CreateFKOneToManyModelWithGeneratedIds()
return modelBuilder.Model.FinalizeModel();
}

private static IModel CreateCyclicFKModel()
private static IModel CreateCyclicFKModel(bool unconstrained = false, bool unconstrainedPrincipalGenerated = false)
{
var modelBuilder = FakeRelationalTestHelpers.Instance.CreateConventionBuilder();

Expand All @@ -1093,16 +1137,34 @@ private static IModel CreateCyclicFKModel()
b.HasIndex(c => c.UniqueValue).IsUnique();
});

modelBuilder.Entity<RelatedFakeEntity>(b => b.HasOne<FakeEntity>()
.WithOne()
.HasForeignKey<RelatedFakeEntity>(c => c.RelatedId));
modelBuilder.Entity<RelatedFakeEntity>(b =>
{
var foreignKeyBuilder = b.HasOne<FakeEntity>()
.WithOne()
.HasForeignKey<RelatedFakeEntity>(c => c.RelatedId);

modelBuilder
if (unconstrainedPrincipalGenerated)
{
foreignKeyBuilder.IsConstrained(false);
}

if (unconstrained || unconstrainedPrincipalGenerated)
{
b.Property(c => c.Id).ValueGeneratedNever();
}
});

var foreignKeyBuilder = modelBuilder
.Entity<FakeEntity>()
.HasOne<RelatedFakeEntity>()
.WithOne()
.HasForeignKey<FakeEntity>(c => c.RelatedId);

if (unconstrained)
{
foreignKeyBuilder.IsConstrained(false);
}

return modelBuilder.Model.FinalizeModel();
}

Expand Down
Loading