From e4e47c882121612d26de35811a1a776f9ffb5e87 Mon Sep 17 00:00:00 2001 From: Arthur Vickers Date: Mon, 26 Jun 2017 15:26:06 -0700 Subject: [PATCH] Make setting CurrentValue to null actually set the property to null Issue #7920 Previously we were not setting the property to null if it was marked as required--we were instead setting conceptual null. But when the CLR type is nullable, we don't need conceptual nulls, so just set the property to null. --- .../ChangeTracking/Internal/InternalEntityEntry.cs | 2 +- test/EFCore.Tests/ChangeTracking/PropertyEntryTest.cs | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/EFCore/ChangeTracking/Internal/InternalEntityEntry.cs b/src/EFCore/ChangeTracking/Internal/InternalEntityEntry.cs index 58a11608138..4bfa35d0c50 100644 --- a/src/EFCore/ChangeTracking/Internal/InternalEntityEntry.cs +++ b/src/EFCore/ChangeTracking/Internal/InternalEntityEntry.cs @@ -694,7 +694,7 @@ public virtual void SetProperty([NotNull] IPropertyBase propertyBase, [CanBeNull var writeValue = true; if (asProperty != null - && (!asProperty.IsNullable + && (!asProperty.ClrType.IsNullableType() || asProperty.GetContainingForeignKeys().Any( p => p.DeleteBehavior == DeleteBehavior.Cascade || p.DeleteBehavior == DeleteBehavior.Restrict))) diff --git a/test/EFCore.Tests/ChangeTracking/PropertyEntryTest.cs b/test/EFCore.Tests/ChangeTracking/PropertyEntryTest.cs index 7c0fd13ada6..e074c76e3fc 100644 --- a/test/EFCore.Tests/ChangeTracking/PropertyEntryTest.cs +++ b/test/EFCore.Tests/ChangeTracking/PropertyEntryTest.cs @@ -71,7 +71,7 @@ public void SetValues_with_IsModified_can_mark_a_set_of_values_as_changed() entry.CurrentValues.SetValues(disconnectedEntity); - Assert.Equal("A", trackedEntity.Name); + Assert.Null(trackedEntity.Name); Assert.Equal("NewLongName", trackedEntity.LongName); Assert.False(entry.Property(e => e.Id).IsModified); @@ -81,7 +81,7 @@ public void SetValues_with_IsModified_can_mark_a_set_of_values_as_changed() var internalEntry = entry.GetInfrastructure(); Assert.False(internalEntry.IsConceptualNull(entry.Property(e => e.Id).Metadata)); - Assert.True(internalEntry.IsConceptualNull(entry.Property(e => e.Name).Metadata)); + Assert.False(internalEntry.IsConceptualNull(entry.Property(e => e.Name).Metadata)); Assert.False(internalEntry.IsConceptualNull(entry.Property(e => e.LongName).Metadata)); foreach (var property in entry.Properties) @@ -223,14 +223,12 @@ public void Can_set_current_value_to_null() new PropertyEntry(entry, "RequiredPrimate").CurrentValue = null; Assert.Null(entity.Primate); - Assert.True(entry.IsConceptualNull(new PropertyEntry(entry, "RequiredPrimate").Metadata)); - Assert.Equal("Tarsier", entity.RequiredPrimate); + Assert.Null(entity.RequiredPrimate); context.ChangeTracker.DetectChanges(); Assert.Null(entity.Primate); - Assert.True(entry.IsConceptualNull(new PropertyEntry(entry, "RequiredPrimate").Metadata)); - Assert.Equal("Tarsier", entity.RequiredPrimate); + Assert.Null(entity.RequiredPrimate); } }