Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/8.0] Revert behavior to throw when attempting to modify an unconstrained alternate key property #32523

Merged
merged 2 commits into from
Jan 3, 2024
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
60 changes: 48 additions & 12 deletions src/EFCore/ChangeTracking/Internal/NavigationFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ namespace Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
/// </summary>
public class NavigationFixer : INavigationFixer
{
private static readonly bool UseOldBehavior32383 =
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue32383", out var enabled32383) && enabled32383;

private IList<(
InternalEntityEntry Entry,
InternalEntityEntry OtherEntry,
Expand Down Expand Up @@ -1412,14 +1415,51 @@ private void ConditionallyNullForeignKeyProperties(
}
}

if (foreignKey.IsRequired
&& hasOnlyKeyProperties
&& dependentEntry.EntityState != EntityState.Detached
&& dependentEntry.EntityState != EntityState.Deleted)
if (UseOldBehavior32383)
{
if (foreignKey.DeleteBehavior == DeleteBehavior.Cascade
|| foreignKey.DeleteBehavior == DeleteBehavior.ClientCascade
|| foreignKey.IsOwnership)
if (foreignKey.IsRequired
&& hasOnlyKeyProperties
&& dependentEntry.EntityState != EntityState.Detached
&& dependentEntry.EntityState != EntityState.Deleted)
{
if (foreignKey.DeleteBehavior == DeleteBehavior.Cascade
|| foreignKey.DeleteBehavior == DeleteBehavior.ClientCascade
|| foreignKey.IsOwnership)
{
try
{
_inFixup = true;
switch (dependentEntry.EntityState)
{
case EntityState.Added:
dependentEntry.SetEntityState(EntityState.Detached);
DeleteFixup(dependentEntry);
break;
case EntityState.Unchanged:
case EntityState.Modified:
dependentEntry.SetEntityState(
dependentEntry.SharedIdentityEntry != null ? EntityState.Detached : EntityState.Deleted);
DeleteFixup(dependentEntry);
break;
}
}
finally
{
_inFixup = false;
}
}
else
{
throw new InvalidOperationException(
CoreStrings.KeyReadOnly(dependentProperties.First().Name, dependentEntry.EntityType.DisplayName()));
}
}
}
else
{
if (foreignKey.IsRequired
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge with else

&& hasOnlyKeyProperties
&& dependentEntry.EntityState != EntityState.Detached)
{
try
{
Expand All @@ -1443,11 +1483,7 @@ private void ConditionallyNullForeignKeyProperties(
_inFixup = false;
}
}
else
{
throw new InvalidOperationException(
CoreStrings.KeyReadOnly(dependentProperties.First().Name, dependentEntry.EntityType.DisplayName()));
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1962,7 +1962,7 @@ private static SecondLaw AddSecondLevel(bool thirdLevel1, bool thirdLevel2)
return secondLevel;
}

[ConditionalTheory] // Issue #28961
[ConditionalTheory] // Issue #28961 and Issue #32385
[InlineData(false)]
[InlineData(true)]
public virtual Task Alternate_key_over_foreign_key_doesnt_bypass_delete_behavior(bool async)
Expand All @@ -1976,16 +1976,14 @@ public virtual Task Alternate_key_over_foreign_key_doesnt_bypass_delete_behavior
? await context.SaveChangesAsync()
: context.SaveChanges();

Assert.Equal(
CoreStrings.KeyReadOnly(nameof(SneakyChild.ParentId), nameof(SneakyChild)),
(await Assert.ThrowsAsync<InvalidOperationException>(
async () =>
{
parent.Children.Remove(parent.Children.First());
_ = async
? await context.SaveChangesAsync()
: context.SaveChanges();
})).Message);
Assert.Equal(2, context.ChangeTracker.Entries().Count());

parent.Children.Remove(parent.Children.First());
_ = async
? await context.SaveChangesAsync()
: context.SaveChanges();

Assert.Equal(1, context.ChangeTracker.Entries().Count());
});

[ConditionalTheory] // Issue #30764
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -856,9 +856,7 @@ public virtual void Save_required_one_to_one_changed_by_reference(

if (Fixture.ForceClientNoAction)
{
Assert.Equal(
CoreStrings.KeyReadOnly(nameof(RequiredSingle1.Id), nameof(RequiredSingle1)),
Assert.Throws<InvalidOperationException>(() => context.SaveChanges()).Message);
Assert.Throws<DbUpdateException>(() => context.SaveChanges());
}
else
{
Expand Down Expand Up @@ -1202,11 +1200,13 @@ public virtual void Sever_required_one_to_one(
throw new ArgumentOutOfRangeException(nameof(changeMechanism));
}

Assert.False(context.Entry(root).Reference(e => e.RequiredSingle).IsLoaded);
Assert.False(context.Entry(old1).Reference(e => e.Root).IsLoaded);
Assert.True(context.ChangeTracker.HasChanges());

if (Fixture.ForceClientNoAction)
{
Assert.Equal(
CoreStrings.KeyReadOnly(nameof(RequiredSingle1.Id), nameof(RequiredSingle1)),
Assert.Throws<InvalidOperationException>(() => context.SaveChanges()).Message);
Assert.Throws<DbUpdateException>(() => context.SaveChanges());
}
else
{
Expand Down
Loading