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

Persist non-implied properties in owned collection types #17457

Merged
merged 1 commit into from
Aug 27, 2019
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
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