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 leave unknown FK values when principal is known but has not-set, non-generated, value #23875

Merged
merged 1 commit into from
Jan 15, 2021
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
27 changes: 13 additions & 14 deletions src/EFCore/ChangeTracking/Internal/KeyPropagator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ public virtual InternalEntityEntry PropagateValue(InternalEntityEntry entry, IPr
{
Check.DebugAssert(property.IsForeignKey(), $"property {property} is not part of an FK");

var principalEntry = TryPropagateValue(entry, property);
var generationProperty = property.FindGenerationProperty();
var principalEntry = TryPropagateValue(entry, property, generationProperty);

if (principalEntry == null
&& property.IsKey()
&& !property.IsForeignKeyToSelf())
{
var valueGenerator = TryGetValueGenerator(property);

var valueGenerator = TryGetValueGenerator(generationProperty);
if (valueGenerator != null)
{
var value = valueGenerator.Next(new EntityEntry(entry));
Expand Down Expand Up @@ -93,12 +94,13 @@ public virtual async Task<InternalEntityEntry> PropagateValueAsync(
{
Check.DebugAssert(property.IsForeignKey(), $"property {property} is not part of an FK");

var principalEntry = TryPropagateValue(entry, property);
var generationProperty = property.FindGenerationProperty();
var principalEntry = TryPropagateValue(entry, property, generationProperty);

if (principalEntry == null
&& property.IsKey())
{
var valueGenerator = TryGetValueGenerator(property);

var valueGenerator = TryGetValueGenerator(generationProperty);
if (valueGenerator != null)
{
var value = await valueGenerator.NextAsync(new EntityEntry(entry), cancellationToken)
Expand All @@ -120,7 +122,7 @@ public virtual async Task<InternalEntityEntry> PropagateValueAsync(
return principalEntry;
}

private static InternalEntityEntry TryPropagateValue(InternalEntityEntry entry, IProperty property)
private static InternalEntityEntry TryPropagateValue(InternalEntityEntry entry, IProperty property, IProperty generationProperty)
{
var entityType = entry.EntityType;
var stateManager = entry.StateManager;
Expand Down Expand Up @@ -158,7 +160,8 @@ private static InternalEntityEntry TryPropagateValue(InternalEntityEntry entry,
if (principalProperty != property)
{
var principalValue = principalEntry[principalProperty];
if (!principalProperty.ClrType.IsDefaultValue(principalValue))
if (generationProperty == null
|| !principalProperty.ClrType.IsDefaultValue(principalValue))
{
if (principalEntry.HasTemporaryValue(principalProperty))
{
Expand All @@ -182,13 +185,9 @@ private static InternalEntityEntry TryPropagateValue(InternalEntityEntry entry,
return null;
}

private ValueGenerator TryGetValueGenerator(IProperty property)
{
var generationProperty = property.FindGenerationProperty();

return generationProperty != null
private ValueGenerator TryGetValueGenerator(IProperty generationProperty)
=> generationProperty != null
? _valueGeneratorSelector.Select(generationProperty, generationProperty.DeclaringEntityType)
: null;
}
}
}
Loading