-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Cosmos: Update pipeline complex properties #37322
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
Merged
AndriySvyryd
merged 9 commits into
dotnet:main
from
JoasE:feature/cosmos-complex-properties-update
Dec 18, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9f0bcbb
Set complex properties in DocumentSource
JoasE 3b4bc43
Update tests
JoasE 2c9dabd
Don't create document sources for owned and complex types
JoasE 4d102b0
Fixes anyPropertyUpdated
JoasE 1c7a5f6
Fix added complex collection item
JoasE 2fdef58
Use document source for owned types (as before)
JoasE 24c58f0
Fix formatting and remove unused code
JoasE c8e2eac
Merge branch 'main' of https://github.com/dotnet/efcore into feature/…
JoasE babdbb0
Rewrite baseline after merge
JoasE File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
src/EFCore/Update/Internal/InternalUpdateEntryExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.EntityFrameworkCore.ChangeTracking.Internal; | ||
|
|
||
| namespace Microsoft.EntityFrameworkCore.Update.Internal; | ||
|
|
||
| /// <summary> | ||
| /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
| /// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
| /// any release. You should only use it directly in your code with extreme caution and knowing that | ||
| /// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
| /// </summary> | ||
| public static class InternalUpdateEntryExtensions | ||
| { | ||
| /// <summary> | ||
| /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
| /// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
| /// any release. You should only use it directly in your code with extreme caution and knowing that | ||
| /// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
| /// </summary> | ||
| public static object? GetCurrentProviderValue(this IInternalEntry updateEntry, IProperty property) | ||
| { | ||
| var value = updateEntry.GetCurrentValue(property); | ||
| var typeMapping = property.GetTypeMapping(); | ||
| value = value?.GetType().IsInteger() == true && typeMapping.ClrType.UnwrapNullableType().IsEnum | ||
| ? Enum.ToObject(typeMapping.ClrType.UnwrapNullableType(), value) | ||
| : value; | ||
|
|
||
| var converter = typeMapping.Converter; | ||
| if (converter != null) | ||
| { | ||
| value = converter.ConvertToProvider(value); | ||
| } | ||
|
|
||
| return value; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 178 additions & 0 deletions
178
test/EFCore.Cosmos.FunctionalTests/CosmosComplexTypesTrackingTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Newtonsoft.Json.Linq; | ||
| using Xunit.Sdk; | ||
|
|
||
| namespace Microsoft.EntityFrameworkCore; | ||
|
|
||
| public class CosmosComplexTypesTrackingTest(CosmosComplexTypesTrackingTest.CosmosFixture fixture) : ComplexTypesTrackingTestBase<CosmosComplexTypesTrackingTest.CosmosFixture>(fixture) | ||
| { | ||
| [ConditionalFact] | ||
| public async Task Can_reorder_complex_collection_elements() | ||
| { | ||
| await using var context = CreateContext(); | ||
| var pub = CreatePubWithCollections(context); | ||
| await context.AddAsync(pub); | ||
| await context.SaveChangesAsync(); | ||
|
|
||
| pub.Activities.Reverse(); | ||
| var first = pub.Activities[0]; | ||
| var last = pub.Activities.Last(); | ||
| await context.SaveChangesAsync(); | ||
|
|
||
| // TODO: Can be asserted after binding has been implemented. | ||
| //await using var assertContext = CreateContext(); | ||
| //var dbPub = await assertContext.Set<PubWithCollections>().FirstAsync(x => x.Id == pub.Id); | ||
| //Assert.Equivalent(first, dbPub.Activities[0]); | ||
| //Assert.Equivalent(last, dbPub.Activities.Last()); | ||
| } | ||
|
|
||
| [ConditionalFact] | ||
| public async Task Can_change_complex_collection_element() | ||
| { | ||
| await using var context = CreateContext(); | ||
| var pub = CreatePubWithCollections(context); | ||
| await context.AddAsync(pub); | ||
| await context.SaveChangesAsync(); | ||
|
|
||
| pub.Activities[0].Name = "Changed123"; | ||
| await context.SaveChangesAsync(); | ||
|
|
||
| // TODO: Can be asserted after binding has been implemented. | ||
| //await using var assertContext = CreateContext(); | ||
| //var dbPub = await assertContext.Set<PubWithCollections>().FirstAsync(x => x.Id == pub.Id); | ||
| //Assert.Equivalent("Changed123", dbPub.Activities[0].Name); | ||
| } | ||
|
|
||
| [ConditionalFact] | ||
| public async Task Can_add_complex_collection_element() | ||
| { | ||
| await using var context = CreateContext(); | ||
| var pub = CreatePubWithCollections(context); | ||
| await context.AddAsync(pub); | ||
| await context.SaveChangesAsync(); | ||
|
|
||
| pub.Activities.Add(new ActivityWithCollection { Name = "NewActivity" }); | ||
| await context.SaveChangesAsync(); | ||
|
|
||
| // TODO: Can be asserted after binding has been implemented. | ||
| //await using var assertContext = CreateContext(); | ||
| //var dbPub = await assertContext.Set<PubWithCollections>().FirstAsync(x => x.Id == pub.Id); | ||
| //Assert.Equivalent("NewActivity", dbPub.Activities.Last().Name); | ||
| //Assert.Equivalent(pub.Activities.Count, dbPub.Activities.Count); | ||
| } | ||
|
|
||
| [ConditionalFact] | ||
| public async Task Can_add_and_dynamically_update_complex_collection_element() | ||
| { | ||
| await using var context = CreateContext(); | ||
| var pub = CreatePubWithCollections(context); | ||
| await context.AddAsync(pub); | ||
| await context.SaveChangesAsync(); | ||
|
|
||
| var pubJObject = context.Entry(pub).Property<JObject>("__jObject").CurrentValue; | ||
| pubJObject["Activities"]![0]!["test"] = "test"; | ||
| pub.Activities.Insert(0, new ActivityWithCollection { Name = "NewActivity" }); | ||
|
|
||
| await context.SaveChangesAsync(); | ||
|
|
||
| await using var assertContext = CreateContext(); | ||
| var dbPub = await assertContext.Set<PubWithCollections>().FirstAsync(x => x.Id == pub.Id); | ||
| var dbPubJObject = assertContext.Entry(dbPub).Property<JObject>("__jObject").CurrentValue; | ||
| Assert.Equal("test", dbPubJObject["Activities"]![1]!["test"]); | ||
| Assert.Equal("NewActivity", dbPubJObject["Activities"]![0]!["Name"]); | ||
| } | ||
|
|
||
| public override Task Can_save_null_second_level_complex_property_with_required_properties(bool async) | ||
| { | ||
| if (!async) | ||
| { | ||
| throw SkipException.ForSkip("Cosmos does not support synchronous operations."); | ||
| } | ||
|
|
||
| return base.Can_save_null_second_level_complex_property_with_required_properties(async); | ||
| } | ||
|
|
||
| public override Task Can_save_null_third_level_complex_property_with_all_optional_properties(bool async) | ||
| { | ||
| if (!async) | ||
| { | ||
| throw SkipException.ForSkip("Cosmos does not support synchronous operations."); | ||
| } | ||
|
|
||
| return base.Can_save_null_third_level_complex_property_with_all_optional_properties(async); | ||
| } | ||
|
|
||
| protected override Task TrackAndSaveTest<TEntity>(EntityState state, bool async, Func<DbContext, TEntity> createPub) | ||
| { | ||
| if (!async) | ||
| { | ||
| throw SkipException.ForSkip("Cosmos does not support synchronous operations."); | ||
| } | ||
|
|
||
| return base.TrackAndSaveTest(state, async, createPub); | ||
| } | ||
|
|
||
| protected override async Task ExecuteWithStrategyInTransactionAsync(Func<DbContext, Task> testOperation, Func<DbContext, Task>? nestedTestOperation1 = null, Func<DbContext, Task>? nestedTestOperation2 = null) | ||
| { | ||
| using var c = CreateContext(); | ||
| await c.Database.CreateExecutionStrategy().ExecuteAsync( | ||
| c, async context => | ||
| { | ||
| using (var innerContext = CreateContext()) | ||
| { | ||
| await testOperation(innerContext); | ||
| } | ||
|
|
||
| if (nestedTestOperation1 == null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| using (var innerContext1 = CreateContext()) | ||
| { | ||
| await nestedTestOperation1(innerContext1); | ||
| } | ||
|
|
||
| if (nestedTestOperation2 == null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| using (var innerContext2 = CreateContext()) | ||
| { | ||
| await nestedTestOperation2(innerContext2); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| public class CosmosFixture : FixtureBase | ||
| { | ||
| protected override ITestStoreFactory TestStoreFactory | ||
| => CosmosTestStoreFactory.Instance; | ||
|
|
||
| protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext context) | ||
| { | ||
| base.OnModelCreating(modelBuilder, context); | ||
| modelBuilder.Entity<Pub>().HasPartitionKey(x => x.Id); | ||
| modelBuilder.Entity<PubWithStructs>().HasPartitionKey(x => x.Id); | ||
| modelBuilder.Entity<PubWithReadonlyStructs>().HasPartitionKey(x => x.Id); | ||
| modelBuilder.Entity<PubWithRecords>().HasPartitionKey(x => x.Id); | ||
| modelBuilder.Entity<PubWithCollections>().HasPartitionKey(x => x.Id); | ||
| modelBuilder.Entity<PubWithRecordCollections>().HasPartitionKey(x => x.Id); | ||
| modelBuilder.Entity<PubWithArrayCollections>().HasPartitionKey(x => x.Id); | ||
| modelBuilder.Entity<PubWithRecordArrayCollections>().HasPartitionKey(x => x.Id); | ||
| modelBuilder.Entity<PubWithPropertyBagCollections>().HasPartitionKey(x => x.Id); | ||
| if (!UseProxies) | ||
| { | ||
| modelBuilder.Entity<FieldPub>().HasPartitionKey(x => x.Id); | ||
| modelBuilder.Entity<FieldPubWithStructs>().HasPartitionKey(x => x.Id); | ||
| modelBuilder.Entity<FieldPubWithRecords>().HasPartitionKey(x => x.Id); | ||
| modelBuilder.Entity<FieldPubWithCollections>().HasPartitionKey(x => x.Id); | ||
| modelBuilder.Entity<FieldPubWithRecordCollections>().HasPartitionKey(x => x.Id); | ||
| modelBuilder.Entity<Yogurt>().HasPartitionKey(x => x.Id); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.