Repository: dotnet/efcore
File: test/EFCore.Specification.Tests/GraphUpdates/GraphUpdatesTestBaseMiscellaneous.cs (C#)

Code before:

                if (async)
                {
                    await context.AddAsync(parent);
                    await context.SaveChangesAsync();
                }
                else
                {
                    context.Add(parent);
                    context.SaveChanges();
                }
            },
            async context =>
            {
                var parent = async
                    ? await context.Set<ParentWithSetDefault>().Include(e => e.Children).SingleAsync()
                    : context.Set<ParentWithSetDefault>().Include(e => e.Children).Single();

                var child = parent.Children.Single();
                Assert.NotEqual(667, child.ParentId);

                context.Remove(parent);

                Assert.Equal(EntityState.Deleted, context.Entry(parent).State);
                Assert.Equal(EntityState.Modified, context.Entry(child).State);
                Assert.Equal(667, child.ParentId); // FK should be set to sentinel value
                Assert.Null(child.Parent);
            });

    [ConditionalTheory, InlineData(false), InlineData(true)]
    public virtual async Task SetDefault_with_sentinel_value_sets_FK_to_sentinel_on_delete(bool async)
        => await ExecuteWithStrategyInTransactionAsync(
            async context =>
            {
                var parent = new ParentWithSetDefaultValue();
                var child = new ChildWithSetDefaultValue { ParentId = 1, Parent = parent };
                parent.Children.Add(child);

                if (async)
                {
                    await context.AddAsync(parent);
                    await context.SaveChangesAsync();
                }
                else
                {
                    context.Add(parent);
                    context.SaveChanges();
                }
            },
            async context =>
            {

Review comment (suggestion):
Additionally, call `SaveChanges` and query this again in another lambda to check it's actually persisted

Code after:
                var parent = new ParentWithClientSetDefault { Id = 1 };
                var child = new ChildWithClientSetDefault { ParentId = 1, Parent = parent };
                parent.Children.Add(child);

                if (async)
                {
                    await context.AddRangeAsync(defaultParent, parent);
                    await context.SaveChangesAsync();
                }
                else
                {
                    context.AddRange(defaultParent, parent);
                    context.SaveChanges();
                }
            },
            async context =>
            {
                var parent = async
                    ? await context.Set<ParentWithClientSetDefault>().Include(e => e.Children).SingleAsync(e => e.Id == 1)
                    : context.Set<ParentWithClientSetDefault>().Include(e => e.Children).Single(e => e.Id == 1);

                var child = parent.Children.Single();
                Assert.Equal(1, child.ParentId);

                context.Remove(parent);

                Assert.Equal(EntityState.Deleted, context.Entry(parent).State);
                Assert.Equal(EntityState.Modified, context.Entry(child).State);
                Assert.Equal(667, child.ParentId); // FK should be set to sentinel value (the default parent)
                Assert.Null(child.Parent);

                if (async)
                {
                    await context.SaveChangesAsync();
                }
                else
                {
                    context.SaveChanges();
                }
            },
            async context =>
            {
                var child = async
                    ? await context.Set<ChildWithClientSetDefault>().SingleAsync()
                    : context.Set<ChildWithClientSetDefault>().Single();

                Assert.Equal(667, child.ParentId); // Verify FK was persisted with sentinel value
            });

    [ConditionalTheory, InlineData(false, false), InlineData(true, false), InlineData(false, true), InlineData(true, true)]
    public virtual Task Can_insert_when_bool_PK_in_composite_key_has_sentinel_value(bool async, bool initialValue)