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

Cosmos: Accept changes for root entities that were not modified but end up being changed due to ETag update #29091

Merged
merged 1 commit into from
Sep 14, 2022
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: 2 additions & 0 deletions src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public override int SaveChanges(IList<IUpdateEntry> entries)
// #16707
((InternalEntityEntry)root).SetEntityState(EntityState.Modified);
#pragma warning restore EF1001 // Internal EF Core API usage.
entries.Add(root);
}

continue;
Expand Down Expand Up @@ -151,6 +152,7 @@ public override async Task<int> SaveChangesAsync(
// #16707
((InternalEntityEntry)root).SetEntityState(EntityState.Modified);
#pragma warning restore EF1001 // Internal EF Core API usage.
entries.Add(root);
}

continue;
Expand Down
100 changes: 84 additions & 16 deletions test/EFCore.Cosmos.FunctionalTests/CosmosConcurrencyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ public virtual Task Updating_then_updating_the_same_entity_results_in_DbUpdateCo
[ConditionalFact]
public async Task Etag_will_return_when_content_response_enabled_false()
{
await using var testDatabase = CosmosTestStore.CreateInitialized(DatabaseName);
await using var testDatabase = CosmosTestStore.CreateInitialized(
DatabaseName,
o => o.ContentResponseOnWriteEnabled(false));

var customer = new Customer
{
Id = "4", Name = "Theon",
};

await using (var context = new ConcurrencyContext(CreateOptions(testDatabase, enableContentResponseOnWrite: false)))
await using (var context = new ConcurrencyContext(CreateOptions(testDatabase)))
{
await context.Database.EnsureCreatedAsync();

Expand All @@ -64,7 +66,7 @@ public async Task Etag_will_return_when_content_response_enabled_false()
await context.SaveChangesAsync();
}

await using (var context = new ConcurrencyContext(CreateOptions(testDatabase, enableContentResponseOnWrite: false)))
await using (var context = new ConcurrencyContext(CreateOptions(testDatabase)))
{
var customerFromStore = await context.Set<Customer>().SingleAsync();

Expand All @@ -81,14 +83,16 @@ public async Task Etag_will_return_when_content_response_enabled_false()
[ConditionalFact]
public async Task Etag_will_return_when_content_response_enabled_true()
{
await using var testDatabase = CosmosTestStore.Create(DatabaseName);
await using var testDatabase = CosmosTestStore.CreateInitialized(
DatabaseName,
o => o.ContentResponseOnWriteEnabled());

var customer = new Customer
{
Id = "3", Name = "Theon",
};

await using (var context = new ConcurrencyContext(CreateOptions(testDatabase, enableContentResponseOnWrite: true)))
await using (var context = new ConcurrencyContext(CreateOptions(testDatabase)))
{
await context.Database.EnsureCreatedAsync();

Expand All @@ -97,7 +101,7 @@ public async Task Etag_will_return_when_content_response_enabled_true()
await context.SaveChangesAsync();
}

await using (var context = new ConcurrencyContext(CreateOptions(testDatabase, enableContentResponseOnWrite: true)))
await using (var context = new ConcurrencyContext(CreateOptions(testDatabase)))
{
var customerFromStore = await context.Set<Customer>().SingleAsync();

Expand All @@ -111,6 +115,70 @@ public async Task Etag_will_return_when_content_response_enabled_true()
}
}

[ConditionalTheory]
[InlineData(null)]
[InlineData(true)]
[InlineData(false)]
public async Task Etag_is_updated_in_entity_after_SaveChanges(bool? contentResponseOnWriteEnabled)
{
await using var testDatabase = CosmosTestStore.CreateInitialized(
DatabaseName,
o =>
{
if (contentResponseOnWriteEnabled.HasValue)
{
o.ContentResponseOnWriteEnabled(contentResponseOnWriteEnabled.Value);
}
});

var customer = new Customer
{
Id = "5",
Name = "Theon",
Children = { new DummyChild { Id = "0" } }
};

string etag = null;

await using (var context = new ConcurrencyContext(CreateOptions(testDatabase)))
{
await context.Database.EnsureCreatedAsync();

context.Add(customer);

await context.SaveChangesAsync();

etag = customer.ETag;
}

await using (var context = new ConcurrencyContext(CreateOptions(testDatabase)))
{
var customerFromStore = await context.Set<Customer>().SingleAsync();

Assert.NotEmpty(customerFromStore.ETag);
Assert.Equal(etag, customerFromStore.ETag);

customerFromStore.Children.Add(new DummyChild { Id = "1" });

await context.SaveChangesAsync();

Assert.NotEmpty(customerFromStore.ETag);
Assert.NotEqual(etag, customerFromStore.ETag);

customerFromStore.Children.Add(new DummyChild { Id = "2" });

Assert.NotEmpty(customerFromStore.ETag);
Assert.NotEqual(etag, customerFromStore.ETag);

customerFromStore.Children.Add(new DummyChild { Id = "3" });

await context.SaveChangesAsync();

Assert.NotEmpty(customerFromStore.ETag);
Assert.NotEqual(etag, customerFromStore.ETag);
}
}

/// <summary>
/// Runs the two actions with two different contexts and calling
/// SaveChanges such that storeChange will succeed and the store will reflect this change, and
Expand Down Expand Up @@ -186,21 +254,14 @@ protected override void OnModelCreating(ModelBuilder builder)
{
b.HasKey(c => c.Id);
b.Property(c => c.ETag).IsETagConcurrency();
b.OwnsMany(x => x.Children);
});
}

private DbContextOptions CreateOptions(CosmosTestStore testDatabase, bool enableContentResponseOnWrite)
{
var optionsBuilder = new DbContextOptionsBuilder();

new DbContextOptionsBuilder().UseCosmos(
testDatabase.ConnectionString, testDatabase.Name,
b => b.ApplyConfiguration().ContentResponseOnWriteEnabled(enabled: enableContentResponseOnWrite));

return testDatabase.AddProviderOptions(optionsBuilder)
private DbContextOptions CreateOptions(CosmosTestStore testDatabase)
=> testDatabase.AddProviderOptions(new DbContextOptionsBuilder())
.EnableDetailedErrors()
.Options;
}

public class Customer
{
Expand All @@ -209,5 +270,12 @@ public class Customer
public string Name { get; set; }

public string ETag { get; set; }

public ICollection<DummyChild> Children { get; } = new HashSet<DummyChild>();
}

public class DummyChild
{
public string Id { get; init; }
}
}