Skip to content

Commit

Permalink
Avoid performing empty updates
Browse files Browse the repository at this point in the history
Fixes #4814
  • Loading branch information
AndriySvyryd committed Mar 29, 2016
1 parent 645e9a7 commit 705b3fa
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected virtual IEnumerable<ModificationCommand> CreateModificationCommands(

command.AddEntry(e);
return command;
});
}).Where(c => c.EntityState != EntityState.Modified || c.ColumnModifications.Any(m => m.IsWrite));
}

// To avoid violating store constraints the modification commands must be sorted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ protected virtual void AppendUpdateCommand(
{
Check.NotNull(commandStringBuilder, nameof(commandStringBuilder));
Check.NotEmpty(name, nameof(name));
Check.NotNull(writeOperations, nameof(writeOperations));
Check.NotEmpty(writeOperations, nameof(writeOperations));
Check.NotNull(conditionOperations, nameof(conditionOperations));

AppendUpdateCommandHeader(commandStringBuilder, name, schema, writeOperations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Microsoft.EntityFrameworkCore.FunctionalTests.TestModels.ConcurrencyModel;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Update;
using Xunit;

namespace Microsoft.EntityFrameworkCore.FunctionalTests
Expand Down Expand Up @@ -125,6 +124,39 @@ public abstract class OptimisticConcurrencyTestBase<TTestStore, TFixture> : ICla
where TTestStore : TestStore
where TFixture : F1FixtureBase<TTestStore>, new()
{
[Fact]
public virtual async Task Modifying_concurrency_token_only_is_noop()
{
byte[] firstVersion;
using (var context = CreateF1Context())
{
var driver = context.Drivers.Single(d => d.CarNumber == 1);
Assert.NotEqual(1, driver.Version[0]);
driver.Podiums = StorePodiums;
firstVersion = driver.Version;
await context.SaveChangesAsync();
}

byte[] secondVersion;
using (var context = CreateF1Context())
{
var driver = context.Drivers.Single(d => d.CarNumber == 1);
Assert.NotEqual(firstVersion, driver.Version);
Assert.Equal(StorePodiums, driver.Podiums);

secondVersion = driver.Version;
driver.Version = firstVersion;
await context.SaveChangesAsync();
}

using (var validationContext = CreateF1Context())
{
var driver = validationContext.Drivers.Single(d => d.CarNumber == 1);
Assert.Equal(secondVersion, driver.Version);
Assert.Equal(StorePodiums, driver.Podiums);
}
}

#region Concurrency resolution with FK associations

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,14 @@ public void BatchCommands_sorts_added_and_related_modified_entities()
{
var configuration = CreateContextServices(CreateSimpleFKModel());
var stateManager = configuration.GetRequiredService<IStateManager>();
var model = configuration.GetRequiredService<IModel>();

var entry = stateManager.GetOrCreateEntry(new FakeEntity { Id = 42, Value = "Test" });
entry.SetEntityState(EntityState.Added);

var relatedentry = stateManager.GetOrCreateEntry(new RelatedFakeEntity { Id = 42 });
relatedentry.SetEntityState(EntityState.Modified);
relatedentry.SetPropertyModified(relatedentry.EntityType.FindProperty(nameof(RelatedFakeEntity.RelatedId)));

var commandBatches = CreateCommandBatchPreparer().BatchCommands(new[] { relatedentry, entry }).ToArray();

Expand Down Expand Up @@ -381,6 +383,7 @@ private static IModel CreateSimpleFKModel()
b.HasOne<FakeEntity>()
.WithOne()
.HasForeignKey<RelatedFakeEntity>(c => c.Id);
b.Property(c => c.RelatedId);
});

return modelBuilder.Model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public OptimisticConcurrencySqliteTest(F1SqliteFixture fixture)
// Override failing tests because SQLite does not allow store-generated row versions.
// Row version behavior could be imitated on SQLite. See Issue #2195
// TODO move these tests into the testing just for SqlServer since they don't apply to SQLite
public override Task Modifying_concurrency_token_only_is_noop() => Task.FromResult(true);
public override Task Simple_concurrency_exception_can_be_resolved_with_store_values() => Task.FromResult(true);
public override Task Simple_concurrency_exception_can_be_resolved_with_client_values() => Task.FromResult(true);
public override Task Simple_concurrency_exception_can_be_resolved_with_new_values() => Task.FromResult(true);
Expand Down

0 comments on commit 705b3fa

Please sign in to comment.