Skip to content
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
9 changes: 9 additions & 0 deletions src/EFCore.Relational/Update/ModificationCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1217,10 +1217,14 @@ public override string ToString()

private sealed class ColumnValuePropagator
{
private static readonly bool UseOldBehavior39617 =
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue39617", out var enabled) && enabled;

private bool _write;
private object? _originalValue;
private object? _currentValue;
private bool _originalValueInitialized;
private bool _currentValueFromDeleted;

public IColumnModification? ColumnModification { get; set; }

Expand All @@ -1236,14 +1240,17 @@ public void RecordValue(IColumnMapping mapping, IUpdateEntry entry)
{
_write = true;
_currentValue = Update.ColumnModification.GetCurrentProviderValue(entry, property);
_currentValueFromDeleted = false;
_originalValue = Update.ColumnModification.GetOriginalProviderValue(entry, property);
_originalValueInitialized = true;
}

break;
case EntityState.Added:
var currentProviderValue = Update.ColumnModification.GetCurrentProviderValue(entry, property);
// A deleted entry's null is not a value from a current writer.
if (_originalValueInitialized
&& (UseOldBehavior39617 || !_currentValueFromDeleted)
&& mapping.Column.ProviderValueComparer.Equals(_originalValue, currentProviderValue))
{
// This Added entry has the same value as the original (pre-modification) value.
Expand All @@ -1257,6 +1264,7 @@ public void RecordValue(IColumnMapping mapping, IUpdateEntry entry)
property.Sentinel))
{
_currentValue = currentProviderValue;
_currentValueFromDeleted = false;
}

_write = true;
Expand All @@ -1270,6 +1278,7 @@ public void RecordValue(IColumnMapping mapping, IUpdateEntry entry)
{
_write = true;
_currentValue = null;
_currentValueFromDeleted = true;
}

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,194 @@ public class RgbColor38917
public WorkItemGroup38917? Group { get; set; }
}

[Theory, InlineData(false, false), InlineData(false, true), InlineData(true, false), InlineData(true, true)] // Issue #39617
public virtual async Task Replacing_owned_entity_preserves_on_add_values(bool async, bool changeFloor)
{
var contextFactory = await InitializeNonSharedTest<DbContext>(
onModelCreating: mb => mb.Entity<Customer39617>(b =>
{
b.Property(e => e.Id).ValueGeneratedNever();
b.OwnsOne(e => e.Address, a =>
{
a.Property(e => e.Floor).HasDefaultValue(1);
a.Property(e => e.NullableFloor).HasDefaultValue(1);
a.Property(e => e.Note).ValueGeneratedOnAdd();
});
}));

var floor = changeFloor ? 4 : 3;
await ExecuteWithStrategyInTransactionAsync(
contextFactory,
async context =>
{
context.Add(
new Customer39617
{
Id = 1,
Address = new Address39617 { City = "Paris", Floor = 3, NullableFloor = 7, Note = "back door" }
});
await context.SaveChangesAsync();
},
async context =>
{
var customer = await context.Set<Customer39617>().SingleAsync();
customer.Address = new Address39617 { City = "Lyon", Floor = floor, NullableFloor = 7, Note = "back door" };

if (async)
{
await context.SaveChangesAsync();
}
else
{
context.SaveChanges();
}

Assert.Equal(floor, customer.Address.Floor);
Assert.Equal(7, customer.Address.NullableFloor);
Assert.Equal("back door", customer.Address.Note);
},
async context =>
{
var customer = await context.Set<Customer39617>().SingleAsync();
Assert.Equal("Lyon", customer.Address.City);
Assert.Equal(floor, customer.Address.Floor);
Assert.Equal(7, customer.Address.NullableFloor);
Assert.Equal("back door", customer.Address.Note);
});
}

[Theory, InlineData(false, false), InlineData(false, true), InlineData(true, false), InlineData(true, true)] // Issue #39617
public virtual async Task Readding_entity_with_same_key_preserves_on_add_values(bool async, bool changeQuantity)
{
var contextFactory = await InitializeNonSharedTest<DbContext>(
onModelCreating: mb => mb.Entity<OrderLine39617>(b =>
{
b.HasKey(e => new { e.OrderId, e.ProductId });
b.Property(e => e.Quantity).HasDefaultValue(1);
b.Property(e => e.Note).HasDefaultValue("none");
}));

var quantity = changeQuantity ? 6 : 5;
await ExecuteWithStrategyInTransactionAsync(
contextFactory,
async context =>
{
context.Add(new OrderLine39617 { OrderId = 1, ProductId = 10, Quantity = 5, Note = "original" });
await context.SaveChangesAsync();
},
async context =>
{
context.Remove(await context.Set<OrderLine39617>().SingleAsync());
var line = new OrderLine39617 { OrderId = 1, ProductId = 10, Quantity = quantity, Note = "original" };
context.Add(line);

if (async)
{
await context.SaveChangesAsync();
}
else
{
context.SaveChanges();
}

Assert.Equal(quantity, line.Quantity);
Assert.Equal("original", line.Note);
},
async context =>
{
var line = await context.Set<OrderLine39617>().SingleAsync();
Assert.Equal(quantity, line.Quantity);
Assert.Equal("original", line.Note);
});
}

[Theory, InlineData(false, false), InlineData(false, true), InlineData(true, false), InlineData(true, true)] // Issue #39617
public virtual async Task Replacing_owned_entity_preserves_modified_shared_column_value(bool async, bool setNull)
{
var contextFactory = await InitializeNonSharedTest<DbContext>(
onModelCreating: mb => mb.Entity<SharedCustomer39617>(b =>
{
b.Property(e => e.Id).ValueGeneratedNever();
b.Property(e => e.Note).HasColumnName("Note").HasDefaultValue("none");
b.OwnsOne(e => e.Address, a => a.Property(e => e.Note).HasColumnName("Note").HasDefaultValue("none"));
}));

var note = setNull ? null : "updated";
await ExecuteWithStrategyInTransactionAsync(
contextFactory,
async context =>
{
context.Add(
new SharedCustomer39617
{
Id = 1,
Note = "original",
Address = new SharedAddress39617 { City = "Paris", Note = "original" }
});
await context.SaveChangesAsync();
},
async context =>
{
var customer = await context.Set<SharedCustomer39617>().SingleAsync();
customer.Note = note;
customer.Address = new SharedAddress39617 { City = "Lyon", Note = "original" };

if (async)
{
await context.SaveChangesAsync();
}
else
{
context.SaveChanges();
}

Assert.Equal(note, customer.Note);
Assert.Equal(note, customer.Address.Note);
},
async context =>
{
var customer = await context.Set<SharedCustomer39617>().SingleAsync();
Assert.Equal("Lyon", customer.Address.City);
Assert.Equal(note, customer.Note);
Assert.Equal(note, customer.Address.Note);
});
}

private class Customer39617
{
public int Id { get; set; }
public Address39617 Address { get; set; } = null!;
}

private class Address39617
{
public string City { get; set; } = "";
public int Floor { get; set; }
public int? NullableFloor { get; set; }
public string? Note { get; set; }
}

private class OrderLine39617
{
public int OrderId { get; set; }
public int ProductId { get; set; }
public int Quantity { get; set; }
public string? Note { get; set; }
}

private class SharedCustomer39617
{
public int Id { get; set; }
public string? Note { get; set; }
public SharedAddress39617 Address { get; set; } = null!;
}

private class SharedAddress39617
{
public string City { get; set; } = "";
public string? Note { get; set; }
}

[Theory, MemberData(nameof(IsAsyncData))]
public virtual async Task Update_entity_with_not_loaded_property_excludes_column_from_SQL(bool async)
{
Expand Down
Loading