Skip to content

Commit

Permalink
Persist non-implied properties in owned collection types
Browse files Browse the repository at this point in the history
Fixes #17345
  • Loading branch information
AndriySvyryd committed Aug 27, 2019
1 parent 24b9aa1 commit e6ff4d1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/EFCore.Cosmos/Extensions/CosmosPropertyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
{
Expand Down
70 changes: 64 additions & 6 deletions test/EFCore.Cosmos.FunctionalTests/EmbeddedDocumentsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Vehicle>(
eb =>
{
eb.OwnsOne(v => v.Operator).OwnsOne(v => v.Details);
});
modelBuilder.Entity<Person>(
eb => eb.OwnsMany(
v => v.Addresses, b =>
{
b.Property<Guid>("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<Person>().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()
{
Expand All @@ -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())
{
Expand Down Expand Up @@ -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())
{
Expand Down Expand Up @@ -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<ModelBuilder> onModelCreating = null)
protected TestStore CreateTestStore(Action<ModelBuilder> onModelCreating = null, bool seed = true)
{
TestStore = TestStoreFactory.Create(DatabaseName);

Expand All @@ -308,7 +359,14 @@ protected TestStore CreateTestStore(Action<ModelBuilder> onModelCreating = null)
.AddSingleton<ILoggerFactory>(TestSqlLoggerFactory)
.BuildServiceProvider(validateScopes: true);

TestStore.Initialize(ServiceProvider, CreateContext, c => ((TransportationContext)c).Seed());

TestStore.Initialize(ServiceProvider, CreateContext, c =>
{
if (seed)
{
((TransportationContext)c).Seed();
}
});

TestSqlLoggerFactory.Clear();

Expand Down Expand Up @@ -383,7 +441,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
private class Person
{
public int Id { get; set; }
public ICollection<Address> Addresses { get; set; }
public ICollection<Address> Addresses { get; set; } = new HashSet<Address>();
}

public class Address
Expand Down

0 comments on commit e6ff4d1

Please sign in to comment.