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

Don't check for temporary values on non-key properties when deleting an entity #22419

Merged
merged 1 commit into from
Sep 5, 2020
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
2 changes: 1 addition & 1 deletion src/EFCore/ChangeTracking/Internal/InternalEntityEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private void SetEntityState(EntityState oldState, EntityState newState, bool acc
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (var property in entityType.GetProperties())
{
if (!property.IsForeignKey() && HasTemporaryValue(property))
if (property.IsKey() && HasTemporaryValue(property))
{
throw new InvalidOperationException(
CoreStrings.TempValuePersists(
Expand Down
30 changes: 25 additions & 5 deletions test/EFCore.Specification.Tests/StoreGeneratedTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Microsoft.EntityFrameworkCore.ValueGeneration.Internal;
using Xunit;

// ReSharper disable InconsistentNaming
Expand Down Expand Up @@ -515,6 +516,7 @@ protected class NonStoreGenDependent
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
public int? StoreGenPrincipalId { get; set; }
public int HasTemp { get; set; }
public StoreGenPrincipal StoreGenPrincipal { get; set; }
}

Expand All @@ -523,8 +525,10 @@ protected class StoreGenPrincipal
public int Id { get; set; }
}

[ConditionalFact] // Issue #22027
public void Change_state_of_entity_with_temp_FK_does_not_throw()
[ConditionalTheory] // Issue #22027 #14192
[InlineData(EntityState.Modified)]
[InlineData(EntityState.Deleted)]
public void Change_state_of_entity_with_temp_non_key_does_not_throw(EntityState targetState)
{
ExecuteWithStrategyInTransaction(
context =>
Expand All @@ -535,7 +539,13 @@ public void Change_state_of_entity_with_temp_FK_does_not_throw()
};

context.Add(dependent);

Assert.True(context.Entry(dependent).Property(e => e.HasTemp).IsTemporary);

context.SaveChanges();

Assert.False(context.Entry(dependent).Property(e => e.HasTemp).IsTemporary);
Assert.Equal(777, dependent.HasTemp);
},
context =>
{
Expand All @@ -548,17 +558,23 @@ public void Change_state_of_entity_with_temp_FK_does_not_throw()

context.Add(dependent);

context.Entry(dependent).State = EntityState.Modified;
context.Entry(dependent).State = targetState;

Assert.Equal(EntityState.Added, context.Entry(principal).State);
Assert.True(context.Entry(principal).Property(e => e.Id).IsTemporary);
Assert.True(context.Entry(dependent).Property(e => e.HasTemp).IsTemporary);
Assert.True(context.Entry(dependent).Property(e => e.StoreGenPrincipalId).IsTemporary);

context.SaveChanges();

Assert.Equal(EntityState.Unchanged, context.Entry(principal).State);
Assert.Equal(EntityState.Unchanged, context.Entry(dependent).State);

Assert.Equal(
targetState == EntityState.Modified ? EntityState.Unchanged : EntityState.Detached,
context.Entry(dependent).State);

Assert.False(context.Entry(principal).Property(e => e.Id).IsTemporary);
Assert.False(context.Entry(dependent).Property(e => e.HasTemp).IsTemporary);
Assert.False(context.Entry(dependent).Property(e => e.StoreGenPrincipalId).IsTemporary);
});
}
Expand Down Expand Up @@ -1911,7 +1927,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con

modelBuilder.Entity<OptionalProduct>();
modelBuilder.Entity<StoreGenPrincipal>();
modelBuilder.Entity<NonStoreGenDependent>();

modelBuilder.Entity<NonStoreGenDependent>()
.Property(e => e.HasTemp)
.ValueGeneratedOnAddOrUpdate()
.HasValueGenerator<TemporaryIntValueGenerator>();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
b.Property(e => e.NullableBackedIntZeroDefault).HasDefaultValue(0);
});

modelBuilder.Entity<NonStoreGenDependent>().Property(e => e.HasTemp).HasDefaultValue(777);

base.OnModelCreating(modelBuilder, context);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
.ValueGeneratedOnAdd()
.HasDefaultValueSql("randomblob(16)");

modelBuilder.Entity<NonStoreGenDependent>().Property(e => e.HasTemp).HasDefaultValue(777);

base.OnModelCreating(modelBuilder, context);
}
}
Expand Down