Skip to content

Commit

Permalink
Allow FK properties to remain temporary when state changes from Added (
Browse files Browse the repository at this point in the history
  • Loading branch information
ajcvickers authored Aug 25, 2020
1 parent 7607b6f commit dc0cdcc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
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 (HasTemporaryValue(property))
if (!property.IsForeignKey() && HasTemporaryValue(property))
{
throw new InvalidOperationException(
CoreStrings.TempValuePersists(
Expand Down
56 changes: 56 additions & 0 deletions test/EFCore.Specification.Tests/StoreGeneratedTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Diagnostics;
Expand Down Expand Up @@ -509,6 +510,59 @@ public virtual void Identity_property_on_Added_entity_with_temporary_value_gets_
context => Assert.Equal("Banana Joe", context.Set<Gumball>().Single(e => e.Id == id).Identity));
}

protected class NonStoreGenDependent
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
public int? StoreGenPrincipalId { get; set; }
public StoreGenPrincipal StoreGenPrincipal { get; set; }
}

protected class StoreGenPrincipal
{
public int Id { get; set; }
}

[ConditionalFact] // Issue #22027
public void Change_state_of_entity_with_temp_FK_does_not_throw()
{
ExecuteWithStrategyInTransaction(
context =>
{
var dependent = new NonStoreGenDependent
{
Id = 89,
};
context.Add(dependent);
context.SaveChanges();
},
context =>
{
var principal = new StoreGenPrincipal();
var dependent = new NonStoreGenDependent
{
Id = 89,
StoreGenPrincipal = principal
};
context.Add(dependent);
context.Entry(dependent).State = EntityState.Modified;
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.StoreGenPrincipalId).IsTemporary);
context.SaveChanges();
Assert.Equal(EntityState.Unchanged, context.Entry(principal).State);
Assert.Equal(EntityState.Unchanged, context.Entry(dependent).State);
Assert.False(context.Entry(principal).Property(e => e.Id).IsTemporary);
Assert.False(context.Entry(dependent).Property(e => e.StoreGenPrincipalId).IsTemporary);
});
}

[ConditionalFact] // Issue #19137
public void Clearing_optional_FK_does_not_leave_temporary_value()
{
Expand Down Expand Up @@ -1856,6 +1910,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
});

modelBuilder.Entity<OptionalProduct>();
modelBuilder.Entity<StoreGenPrincipal>();
modelBuilder.Entity<NonStoreGenDependent>();
}
}
}
Expand Down

0 comments on commit dc0cdcc

Please sign in to comment.