From e6ff4d11ef71f8a641ef5680151cff4bd3f33171 Mon Sep 17 00:00:00 2001 From: AndriySvyryd Date: Mon, 26 Aug 2019 16:57:47 -0700 Subject: [PATCH] Persist non-implied properties in owned collection types Fixes #17345 --- .../Extensions/CosmosPropertyExtensions.cs | 1 + .../EmbeddedDocumentsTest.cs | 70 +++++++++++++++++-- 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/src/EFCore.Cosmos/Extensions/CosmosPropertyExtensions.cs b/src/EFCore.Cosmos/Extensions/CosmosPropertyExtensions.cs index c7fc852d8f1..4be7657f8e4 100644 --- a/src/EFCore.Cosmos/Extensions/CosmosPropertyExtensions.cs +++ b/src/EFCore.Cosmos/Extensions/CosmosPropertyExtensions.cs @@ -33,6 +33,7 @@ private static string GetDefaultPropertyName(IProperty property) { var pk = property.FindContainingPrimaryKey(); if (pk != null + && (property.ClrType == typeof(int) || ownership.Properties.Contains(property)) && pk.Properties.Count == ownership.Properties.Count + (ownership.IsUnique ? 0 : 1) && ownership.Properties.All(fkProperty => pk.Properties.Contains(fkProperty))) { diff --git a/test/EFCore.Cosmos.FunctionalTests/EmbeddedDocumentsTest.cs b/test/EFCore.Cosmos.FunctionalTests/EmbeddedDocumentsTest.cs index 65b21cc7c4e..26bb25c92e9 100644 --- a/test/EFCore.Cosmos.FunctionalTests/EmbeddedDocumentsTest.cs +++ b/test/EFCore.Cosmos.FunctionalTests/EmbeddedDocumentsTest.cs @@ -83,7 +83,7 @@ public virtual async Task Can_update_owner_with_dependents() [ConditionalFact] public virtual async Task Can_add_collection_dependent_to_owner() { - await using (var testDatabase = CreateTestStore()) + await using (var testDatabase = CreateTestStore(seed: false)) { Address existingAddress1Person2; Address existingAddress1Person3; @@ -169,6 +169,57 @@ public virtual async Task Can_add_collection_dependent_to_owner() } } + [ConditionalFact] + public virtual async Task Can_use_non_int_keys_for_embedded_entities() + { + await using (var testDatabase = CreateTestStore( + modelBuilder => + { + modelBuilder.Entity( + eb => + { + eb.OwnsOne(v => v.Operator).OwnsOne(v => v.Details); + }); + + modelBuilder.Entity( + eb => eb.OwnsMany( + v => v.Addresses, b => + { + b.Property("Id"); + })); + }, seed: false)) + { + Address address; + Guid addressGuid; + await using (var context = CreateContext()) + { + context.Database.EnsureCreated(); + var person = new Person { Id = 1 }; + address = new Address { Street = "Second", City = "Village" }; + person.Addresses.Add(address); + context.Add(person); + + var addressEntry = context.Entry(address); + addressGuid = (Guid)addressEntry.Property("Id").CurrentValue; + + await context.SaveChangesAsync(); + } + + await using (var context = CreateContext()) + { + var people = await context.Set().OrderBy(o => o.Id).ToListAsync(); + var addresses = people[0].Addresses.ToList(); + Assert.Equal(1, addresses.Count); + + Assert.Equal(address.Street, addresses[0].Street); + Assert.Equal(address.City, addresses[0].City); + + var addressEntry = context.Entry(addresses[0]); + Assert.Equal(addressGuid, (Guid)addressEntry.Property("Id").CurrentValue); + } + } + } + [ConditionalFact] public virtual async Task Can_query_just_nested_reference() { @@ -188,7 +239,7 @@ public virtual async Task Can_query_just_nested_reference() [ConditionalFact] public virtual async Task Can_query_just_nested_collection() { - await using (var testDatabase = CreateTestStore()) + await using (var testDatabase = CreateTestStore(seed: false)) { using (var context = CreateContext()) { @@ -217,7 +268,7 @@ public virtual async Task Can_query_just_nested_collection() [ConditionalFact] public virtual async Task Inserting_dependent_without_principal_throws() { - await using (var testDatabase = CreateTestStore()) + await using (var testDatabase = CreateTestStore(seed: false)) { using (var context = CreateContext()) { @@ -299,7 +350,7 @@ protected void AssertSql(params string[] expected) protected void AssertContainsSql(params string[] expected) => TestSqlLoggerFactory.AssertBaseline(expected, assertOrder: false); - protected TestStore CreateTestStore(Action onModelCreating = null) + protected TestStore CreateTestStore(Action onModelCreating = null, bool seed = true) { TestStore = TestStoreFactory.Create(DatabaseName); @@ -308,7 +359,14 @@ protected TestStore CreateTestStore(Action onModelCreating = null) .AddSingleton(TestSqlLoggerFactory) .BuildServiceProvider(validateScopes: true); - TestStore.Initialize(ServiceProvider, CreateContext, c => ((TransportationContext)c).Seed()); + + TestStore.Initialize(ServiceProvider, CreateContext, c => + { + if (seed) + { + ((TransportationContext)c).Seed(); + } + }); TestSqlLoggerFactory.Clear(); @@ -383,7 +441,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) private class Person { public int Id { get; set; } - public ICollection
Addresses { get; set; } + public ICollection
Addresses { get; set; } = new HashSet
(); } public class Address