Skip to content

Commit

Permalink
Don't issue a warning when replacing an optional dependent with non-n…
Browse files Browse the repository at this point in the history
…ull values

Fixes #26712
  • Loading branch information
AndriySvyryd committed Jan 7, 2022
1 parent 7e94966 commit 58c6d0e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/EFCore.Relational/Update/ModificationCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ private List<IColumnModification> GenerateColumnModifications()
}

var optionalDependentWithAllNull =
(entry.EntityState == EntityState.Deleted
(entry.EntityState == EntityState.Modified
|| entry.EntityState == EntityState.Added)
&& tableMapping.Table.IsOptional(entry.EntityType)
&& tableMapping.Table.GetRowInternalForeignKeys(entry.EntityType).Any();
Expand Down Expand Up @@ -353,11 +353,17 @@ private List<IColumnModification> GenerateColumnModifications()

if (optionalDependentWithAllNull
&& columnModification.IsWrite
&& (entry.EntityState != EntityState.Added || columnModification.Value is not null))
&& columnModification.Value is not null)
{
optionalDependentWithAllNull = false;
}
}
else if (optionalDependentWithAllNull
&& state == EntityState.Modified
&& entry.GetCurrentValue(property) is not null)
{
optionalDependentWithAllNull = false;
}
}

if (optionalDependentWithAllNull && _logger != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,22 @@ public virtual async Task Warn_when_save_optional_dependent_with_null_values()
var log = TestSqlLoggerFactory.Log.Single(l => l.Level == LogLevel.Warning);

Assert.Equal(expected, log.Message);

TestSqlLoggerFactory.Clear();

meterReading.MeterReadingDetails = new MeterReadingDetail { CurrentRead = "100" };

context.SaveChanges();

Assert.Empty(TestSqlLoggerFactory.Log.Where(l => l.Level == LogLevel.Warning));

meterReading.MeterReadingDetails = new MeterReadingDetail();

context.SaveChanges();

log = TestSqlLoggerFactory.Log.Single(l => l.Level == LogLevel.Warning);

Assert.Equal(expected, log.Message);
}

[ConditionalFact]
Expand All @@ -595,9 +611,11 @@ public virtual async Task No_warn_when_save_optional_dependent_at_least_one_none

context.SaveChanges();

var log = TestSqlLoggerFactory.Log.SingleOrDefault(l => l.Level == LogLevel.Warning);
meterReading.MeterReadingDetails = new MeterReadingDetail { CurrentRead = "100" };

context.SaveChanges();

Assert.Null(log.Message);
Assert.Empty(TestSqlLoggerFactory.Log.Where(l => l.Level == LogLevel.Warning));
}

protected override string StoreName { get; } = "TableSplittingTest";
Expand Down Expand Up @@ -684,8 +702,8 @@ public SharedTableContext(DbContextOptions options)
{
}

protected DbSet<MeterReading> MeterReadings { get; set; }
protected DbSet<MeterReadingDetail> MeterReadingDetails { get; set; }
public DbSet<MeterReading> MeterReadings { get; set; }
public DbSet<MeterReadingDetail> MeterReadingDetails { get; set; }
}

protected class MeterReading
Expand Down

0 comments on commit 58c6d0e

Please sign in to comment.