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 issue a warning when replacing an optional dependent with non-null values #27136

Merged
merged 1 commit into from
Jan 7, 2022
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
13 changes: 10 additions & 3 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 @@ -352,12 +352,19 @@ private List<IColumnModification> GenerateColumnModifications()
columnModifications.Add(columnModification);

if (optionalDependentWithAllNull
&& columnModification.IsWrite
&& (entry.EntityState != EntityState.Added || columnModification.Value is not null))
&& (columnModification.IsWrite
|| (columnModification.IsCondition && !isKey))
&& 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