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

Allow setting original/current values using a typed dictionary #25895

Merged
merged 1 commit into from
Sep 8, 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
2 changes: 1 addition & 1 deletion src/EFCore/ChangeTracking/PropertyValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ protected PropertyValues(InternalEntityEntry internalEntry)
/// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see> for more information.
/// </remarks>
/// <param name="values"> The dictionary to read values from. </param>
public virtual void SetValues(IDictionary<string, object?> values)
public virtual void SetValues<TProperty>(IDictionary<string, TProperty> values)
{
Check.NotNull(values, nameof(values));

Expand Down
90 changes: 90 additions & 0 deletions test/EFCore.Specification.Tests/PropertyValuesTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,96 @@ private void TestDictionarySetValues(
ValidateBuildingPropereties(context.Entry(building), getValue, 13, "Pine Walk");
}

[ConditionalFact]
public virtual void Current_values_can_be_set_from_dictionary_typed_int()
{
TestDictionarySetValuesTypedInt(e => e.CurrentValues, (e, n) => e.Property(n).CurrentValue);
}

[ConditionalFact]
public virtual void Original_values_can_be_set_from_dictionary_typed_int()
{
TestDictionarySetValuesTypedInt(e => e.OriginalValues, (e, n) => e.Property(n).OriginalValue);
}

private void TestDictionarySetValuesTypedInt(
Func<EntityEntry, PropertyValues> getPropertyValues,
Func<EntityEntry, string, object> getValue)
{
using var context = CreateContext();
var building = context.Set<Building>().Single(b => b.Name == "Building One");
var buildingValues = getPropertyValues(context.Entry(building));

var dictionary = new Dictionary<string, int>
{
{ "Shadow1", 13 },
{ "PrincipalMailRoomId", 0 }
};

buildingValues.SetValues(dictionary);

Assert.Equal("Building One", buildingValues["Name"]);
Assert.Equal(1500000m, buildingValues["Value"]);
Assert.Equal(13, buildingValues["Shadow1"]);
Assert.Equal("Meadow Drive", buildingValues["Shadow2"]);

Assert.Equal("Building One", getValue(context.Entry(building), "Name"));
Assert.Equal(1500000m, getValue(context.Entry(building), "Value"));
Assert.Equal(13, getValue(context.Entry(building), "Shadow1"));
Assert.Equal("Meadow Drive", getValue(context.Entry(building), "Shadow2"));

Assert.False(context.Entry(building).Property("Name").IsModified);
Assert.False(context.Entry(building).Property("BuildingId").IsModified);
Assert.False(context.Entry(building).Property("Value").IsModified);
Assert.True(context.Entry(building).Property("Shadow1").IsModified);
Assert.False(context.Entry(building).Property("Shadow2").IsModified);
}

[ConditionalFact]
public virtual void Current_values_can_be_set_from_dictionary_typed_string()
{
TestDictionarySetValuesTypedString(e => e.CurrentValues, (e, n) => e.Property(n).CurrentValue);
}

[ConditionalFact]
public virtual void Original_values_can_be_set_from_dictionary_typed_string()
{
TestDictionarySetValuesTypedString(e => e.OriginalValues, (e, n) => e.Property(n).OriginalValue);
}

private void TestDictionarySetValuesTypedString(
Func<EntityEntry, PropertyValues> getPropertyValues,
Func<EntityEntry, string, object> getValue)
{
using var context = CreateContext();
var building = context.Set<Building>().Single(b => b.Name == "Building One");
var buildingValues = getPropertyValues(context.Entry(building));

var dictionary = new Dictionary<string, string>
{
{ "Name", "Values End" },
{ "Shadow2", "Pine Walk" },
};

buildingValues.SetValues(dictionary);

Assert.Equal("Values End", buildingValues["Name"]);
Assert.Equal(1500000m, buildingValues["Value"]);
Assert.Equal(11, buildingValues["Shadow1"]);
Assert.Equal("Pine Walk", buildingValues["Shadow2"]);

Assert.Equal("Values End", getValue(context.Entry(building), "Name"));
Assert.Equal(1500000m, getValue(context.Entry(building), "Value"));
Assert.Equal(11, getValue(context.Entry(building), "Shadow1"));
Assert.Equal("Pine Walk", getValue(context.Entry(building), "Shadow2"));

Assert.True(context.Entry(building).Property("Name").IsModified);
Assert.False(context.Entry(building).Property("BuildingId").IsModified);
Assert.False(context.Entry(building).Property("Value").IsModified);
Assert.False(context.Entry(building).Property("Shadow1").IsModified);
Assert.True(context.Entry(building).Property("Shadow2").IsModified);
}

[ConditionalFact]
public virtual void Current_values_can_be_set_from_dictionary_some_missing()
{
Expand Down