Skip to content

Commit a154024

Browse files
authored
Merge pull request #37382 from dotnet/Issue37373
[release/10.0] Fix deletion of entities with non-empty complex collection Fixes #37373 Port of #37377
2 parents 2f8a4d9 + f59b481 commit a154024

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/EFCore.Relational/Update/ModificationCommand.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public class ModificationCommand : IModificationCommand, INonTrackedModification
3737
private EntityState _entityState;
3838
private readonly IDiagnosticsLogger<DbLoggerCategory.Update>? _logger;
3939

40+
private static readonly bool UseOldBehavior37373 =
41+
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue37373", out var enabled) && enabled;
42+
4043
/// <summary>
4144
/// Initializes a new <see cref="ModificationCommand" /> instance.
4245
/// </summary>
@@ -288,7 +291,7 @@ private List<IColumnModification> GenerateColumnModifications()
288291
}
289292
}
290293

291-
if (_entries.Any(e => e.EntityType is { } entityType
294+
if ((!deleting || UseOldBehavior37373) && _entries.Any(e => e.EntityType is { } entityType
292295
&& (entityType.IsMappedToJson()
293296
|| entityType.GetFlattenedComplexProperties().Any(cp => cp.ComplexType.IsMappedToJson())
294297
|| entityType.GetNavigations().Any(e => e.IsCollection && e.TargetEntityType.IsMappedToJson()))))

test/EFCore.Relational.Specification.Tests/Update/ComplexCollectionJsonUpdateTestBase.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,30 @@ public virtual Task Remove_element_from_complex_collection_mapped_to_json()
7070
}
7171
});
7272

73+
[ConditionalFact]
74+
public virtual Task Delete_complex_collection_owner_entity_mapped_to_json()
75+
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
76+
CreateContext,
77+
UseTransaction,
78+
async context =>
79+
{
80+
var company = await context.Companies.SingleAsync(c => c.Id == 1);
81+
82+
context.Remove(company);
83+
84+
ClearLog();
85+
86+
await context.SaveChangesAsync();
87+
},
88+
async context =>
89+
{
90+
using (SuspendRecordingEvents())
91+
{
92+
var exists = await context.Companies.AnyAsync(c => c.Id == 1);
93+
Assert.False(exists);
94+
}
95+
});
96+
7397
[ConditionalFact]
7498
public virtual Task Modify_element_in_complex_collection_mapped_to_json()
7599
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(

test/EFCore.SqlServer.FunctionalTests/Update/ComplexCollectionJsonUpdateSqlServerTest.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ OUTPUT 1
4444
""");
4545
}
4646

47+
public override async Task Delete_complex_collection_owner_entity_mapped_to_json()
48+
{
49+
await base.Delete_complex_collection_owner_entity_mapped_to_json();
50+
51+
AssertSql(
52+
"""
53+
@p0='1'
54+
55+
SET IMPLICIT_TRANSACTIONS OFF;
56+
SET NOCOUNT ON;
57+
DELETE FROM [Companies]
58+
OUTPUT 1
59+
WHERE [Id] = @p0;
60+
""");
61+
}
62+
63+
4764
public override async Task Modify_element_in_complex_collection_mapped_to_json()
4865
{
4966
await base.Modify_element_in_complex_collection_mapped_to_json();

0 commit comments

Comments
 (0)