Skip to content

Commit

Permalink
[2.1.3] Add quirks for in-memory concurrency patch fix
Browse files Browse the repository at this point in the history
Issue #12214
  • Loading branch information
ajcvickers committed Jun 28, 2018
1 parent cb4191d commit 29b94e2
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/EFCore.InMemory/Storage/Internal/InMemoryTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,22 @@ private static bool IsConcurrencyConflict(
object rowValue,
Dictionary<IProperty, object> concurrencyConflicts)
{
if (property.IsConcurrencyToken
&& !StructuralComparisons.StructuralEqualityComparer.Equals(
rowValue,
entry.GetOriginalValue(property)))
if (property.IsConcurrencyToken)
{
concurrencyConflicts.Add(property, rowValue);
var originalValue = entry.GetOriginalValue(property);

return true;
var revertPatchBehavior = AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue12214", out var isEnabled)
&& isEnabled;

if ((revertPatchBehavior
&& !Equals(rowValue, originalValue))
|| (!revertPatchBehavior
&& !StructuralComparisons.StructuralEqualityComparer.Equals(rowValue, originalValue)))
{
concurrencyConflicts.Add(property, rowValue);

return true;
}
}

return false;
Expand Down
74 changes: 74 additions & 0 deletions src/EFCore.Specification.Tests/UpdatesTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,80 @@ public virtual void Remove_on_bytes_concurrency_token_original_value_matches_doe
Assert.Null(context.ProductWithBytes.Find(productId));
});
}

[Fact]
public virtual void Update_on_bytes_concurrency_token_original_value_matches_throws_with_quirk()
{
var productId = new Guid("984ade3c-2f7b-4651-a351-642e92ab7146");

try
{
AppContext.SetSwitch("Microsoft.EntityFrameworkCore.Issue12214", true);

ExecuteWithStrategyInTransaction(
context =>
{
var entry = context.ProductWithBytes.Attach(
new ProductWithBytes
{
Id = productId,
Name = "MegaChips",
Bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }
});
entry.Entity.Name = "GigaChips";
Assert.Throws<DbUpdateConcurrencyException>(
() => context.SaveChanges());
},
context =>
{
Assert.Equal("MegaChips", context.ProductWithBytes.Find(productId).Name);
});

}
finally
{
AppContext.SetSwitch("Microsoft.EntityFrameworkCore.Issue12214", false);
}
}

[Fact]
public virtual void Remove_on_bytes_concurrency_token_original_value_matches_throws_with_quirk()
{
var productId = new Guid("984ade3c-2f7b-4651-a351-642e92ab7146");

try
{
AppContext.SetSwitch("Microsoft.EntityFrameworkCore.Issue12214", true);

ExecuteWithStrategyInTransaction(
context =>
{
var entry = context.ProductWithBytes.Attach(
new ProductWithBytes
{
Id = productId,
Name = "MegaChips",
Bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }
});
entry.State = EntityState.Deleted;
Assert.Throws<DbUpdateConcurrencyException>(
() => context.SaveChanges());
},
context =>
{
Assert.Equal("MegaChips", context.ProductWithBytes.Find(productId).Name);
});

}
finally
{
AppContext.SetSwitch("Microsoft.EntityFrameworkCore.Issue12214", false);
}
}
#endif

[Fact]
Expand Down

0 comments on commit 29b94e2

Please sign in to comment.