Skip to content

Commit

Permalink
Async fix for "Generate values for PK properties that are also self-r…
Browse files Browse the repository at this point in the history
…eferencing FK properties" (#29134)
  • Loading branch information
ajcvickers authored Sep 19, 2022
1 parent 121bb26 commit b02da05
Show file tree
Hide file tree
Showing 31 changed files with 143 additions and 142 deletions.
3 changes: 2 additions & 1 deletion src/EFCore/ChangeTracking/Internal/KeyPropagator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public KeyPropagator(
var principalEntry = TryPropagateValue(entry, property, generationProperty);

if (principalEntry == null
&& property.IsKey())
&& property.IsKey()
&& !property.IsForeignKeyToSelf())
{
var valueGenerator = TryGetValueGenerator(
generationProperty,
Expand Down
16 changes: 8 additions & 8 deletions test/EFCore.Cosmos.FunctionalTests/ConfigPatternsCosmosTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public async Task Should_not_throw_if_specified_region_is_right()
using var context = new CustomerContext(options);
context.Database.EnsureCreated();

context.Add(customer);
await context.AddAsync(customer);

context.SaveChanges();
await context.SaveChangesAsync();
}

[ConditionalFact]
Expand All @@ -78,9 +78,9 @@ public async Task Should_throw_if_specified_region_is_wrong()
using var context = new CustomerContext(options);
context.Database.EnsureCreated();
context.Add(customer);
await context.AddAsync(customer);
context.SaveChanges();
await context.SaveChangesAsync();
});

Assert.Equal(
Expand All @@ -101,9 +101,9 @@ public async Task Should_not_throw_if_specified_connection_mode_is_right()
using var context = new CustomerContext(options);
context.Database.EnsureCreated();

context.Add(customer);
await context.AddAsync(customer);

context.SaveChanges();
await context.SaveChangesAsync();
}

[ConditionalFact]
Expand All @@ -121,9 +121,9 @@ public async Task Should_throw_if_specified_connection_mode_is_wrong()
using var context = new CustomerContext(options);
context.Database.EnsureCreated();
context.Add(customer);
await context.AddAsync(customer);
context.SaveChanges();
await context.SaveChangesAsync();
});
}

Expand Down
6 changes: 3 additions & 3 deletions test/EFCore.Cosmos.FunctionalTests/CosmosConcurrencyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public async Task Etag_will_return_when_content_response_enabled_false()
{
await context.Database.EnsureCreatedAsync();

context.Add(customer);
await context.AddAsync(customer);

await context.SaveChangesAsync();
}
Expand Down Expand Up @@ -96,7 +96,7 @@ public async Task Etag_will_return_when_content_response_enabled_true()
{
await context.Database.EnsureCreatedAsync();

context.Add(customer);
await context.AddAsync(customer);

await context.SaveChangesAsync();
}
Expand Down Expand Up @@ -144,7 +144,7 @@ public async Task Etag_is_updated_in_entity_after_SaveChanges(bool? contentRespo
{
await context.Database.EnsureCreatedAsync();

context.Add(customer);
await context.AddAsync(customer);

await context.SaveChangesAsync();

Expand Down
18 changes: 9 additions & 9 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_attach_owner_with_dependents()
using (var context = new EmbeddedTransportationContext(options))
{
//Issue #15289
var firstVehicleEntry = context.Add(firstVehicle);
var firstVehicleEntry = await context.AddAsync(firstVehicle);
firstVehicleEntry.State = EntityState.Unchanged;
firstVehicle.Operator.Name += "1";

Expand Down Expand Up @@ -112,7 +112,7 @@ public virtual async Task Can_manipulate_embedded_collections(bool useIds)
Address addedAddress3;
using (var context = new EmbeddedTransportationContext(options))
{
context.Add(new Person { Id = 1 });
await context.AddAsync(new Person { Id = 1 });
existingAddress1Person2 = new Address
{
Street = "Second",
Expand Down Expand Up @@ -140,7 +140,7 @@ public virtual async Task Can_manipulate_embedded_collections(bool useIds)
Street = "First",
City = "Village"
};
context.Add(new Person { Id = 2, Addresses = new List<Address> { existingAddress1Person2, existingAddress2Person2 } });
await context.AddAsync(new Person { Id = 2, Addresses = new List<Address> { existingAddress1Person2, existingAddress2Person2 } });
existingAddress1Person3 = new Address
{
Street = "First",
Expand Down Expand Up @@ -169,7 +169,7 @@ public virtual async Task Can_manipulate_embedded_collections(bool useIds)
AddressTitle = new AddressTitle { Title = "P3 Billing" }
};

context.Add(new Person { Id = 3, Addresses = new List<Address> { existingAddress1Person3, existingAddress2Person3 } });
await context.AddAsync(new Person { Id = 3, Addresses = new List<Address> { existingAddress1Person3, existingAddress2Person3 } });

await context.SaveChangesAsync();

Expand Down Expand Up @@ -407,7 +407,7 @@ public virtual async Task Properties_on_owned_types_can_be_client_generated()
AddressTitle = new AddressTitle()
};

context.Add(new Person { Id = 1, Addresses = new List<Address> { address } });
await context.AddAsync(new Person { Id = 1, Addresses = new List<Address> { address } });
Assert.Equal("DefaultTitle", address.AddressTitle.Title);

await context.SaveChangesAsync();
Expand Down Expand Up @@ -441,7 +441,7 @@ public virtual async Task Can_use_non_int_keys_for_embedded_entities()
var person = new Person { Id = 1 };
address = new Address { Street = "Second", City = "Village" };
person.Addresses.Add(address);
context.Add(person);
await context.AddAsync(person);

var addressEntry = context.Entry(address);
addressGuid = (Guid)addressEntry.Property("Id").CurrentValue;
Expand Down Expand Up @@ -505,7 +505,7 @@ public virtual async Task Can_query_just_embedded_collection()

using (var context = new EmbeddedTransportationContext(options))
{
context.Add(
await context.AddAsync(
new Person
{
Id = 3,
Expand All @@ -531,7 +531,7 @@ public virtual async Task Inserting_dependent_without_principal_throws()
{
var options = Fixture.CreateOptions(seed: false);
using var context = new EmbeddedTransportationContext(options);
context.Add(
await context.AddAsync(
new LicensedOperator
{
Name = "Jack Jackson",
Expand Down Expand Up @@ -586,7 +586,7 @@ public virtual async Task Can_change_principal_instance_non_derived()
};

context.Remove(bike);
context.Add(newBike);
await context.AddAsync(newBike);

TestSqlLoggerFactory.Clear();
await context.SaveChangesAsync();
Expand Down
54 changes: 27 additions & 27 deletions test/EFCore.Cosmos.FunctionalTests/EndToEndCosmosTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public async Task Can_add_update_delete_end_to_end_async()
{
await context.Database.EnsureCreatedAsync();

context.Add(customer);
await context.AddAsync(customer);

await context.SaveChangesAsync();

Expand Down Expand Up @@ -170,11 +170,11 @@ public async Task Can_add_update_delete_detached_entity_end_to_end_async()
{
await context.Database.EnsureCreatedAsync();

var entry = context.Add(customer);
var entry = await context.AddAsync(customer);

await context.SaveChangesAsync();

context.Add(customer);
await context.AddAsync(customer);

storeId = entry.Property<string>(StoreKeyConvention.DefaultIdPropertyName).CurrentValue;
}
Expand Down Expand Up @@ -319,7 +319,7 @@ public async Task Can_add_update_untracked_properties_async()
{
await context.Database.EnsureCreatedAsync();

var entry = context.Add(customer);
var entry = await context.AddAsync(customer);

await context.SaveChangesAsync();

Expand All @@ -336,7 +336,7 @@ public async Task Can_add_update_untracked_properties_async()
{
Assert.Empty(await context.Set<Customer>().ToListAsync());

var entry = context.Add(customer);
var entry = await context.AddAsync(customer);

entry.Property<JObject>("__jObject").CurrentValue = new JObject { ["key1"] = "value1" };

Expand Down Expand Up @@ -408,7 +408,7 @@ public async Task Can_add_update_delete_end_to_end_with_Guid_async()
{
await context.Database.EnsureCreatedAsync();

context.Add(customer);
await context.AddAsync(customer);

await context.SaveChangesAsync();
}
Expand Down Expand Up @@ -459,7 +459,7 @@ public async Task Can_add_update_delete_end_to_end_with_DateTime_async()
{
await context.Database.EnsureCreatedAsync();

var entry = context.Add(customer);
var entry = await context.AddAsync(customer);

Assert.Equal("CustomerDateTime|0001-01-01T00:00:00.0000000|Theon^2F^5C^23^5C^5C^3F", entry.CurrentValues["__id"]);

Expand Down Expand Up @@ -588,7 +588,7 @@ public async Task Can_add_update_delete_with_dateTime_string_end_to_end_async()
{
await context.Database.EnsureCreatedAsync();

context.Add(customer);
await context.AddAsync(customer);

await context.SaveChangesAsync();
}
Expand Down Expand Up @@ -828,7 +828,7 @@ private async Task Can_add_update_delete_with_collection<TCollection>(
{
await context.Database.EnsureCreatedAsync();

context.Add(customer);
await context.AddAsync(customer);

await context.SaveChangesAsync();
}
Expand Down Expand Up @@ -911,8 +911,8 @@ public async Task Can_read_with_find_with_resource_id_async()
context.Model.FindEntityType(typeof(CustomerWithResourceId))
.FindProperty(StoreKeyConvention.DefaultIdPropertyName));

context.Add(customer);
context.Add(
await context.AddAsync(customer);
await context.AddAsync(
new CustomerWithResourceId
{
id = "42",
Expand Down Expand Up @@ -1039,8 +1039,8 @@ public async Task Can_read_with_find_with_partition_key_and_value_generator_asyn
{
await context.Database.EnsureCreatedAsync();

context.Add(customer);
context.Add(
await context.AddAsync(customer);
await context.AddAsync(
new Customer
{
Id = 42,
Expand Down Expand Up @@ -1207,7 +1207,7 @@ public async Task Can_read_with_find_with_partition_key_not_part_of_primary_key(
{
await context.Database.EnsureCreatedAsync();

context.Add(customer);
await context.AddAsync(customer);

await context.SaveChangesAsync();
}
Expand All @@ -1233,7 +1233,7 @@ public async Task Can_read_with_find_without_partition_key()
{
await context.Database.EnsureCreatedAsync();

context.Add(customer);
await context.AddAsync(customer);

await context.SaveChangesAsync();
}
Expand All @@ -1259,7 +1259,7 @@ public async Task Can_read_with_find_with_PK_partition_key()
{
await context.Database.EnsureCreatedAsync();

context.Add(customer);
await context.AddAsync(customer);

await context.SaveChangesAsync();
}
Expand All @@ -1285,7 +1285,7 @@ public async Task Can_read_with_find_with_PK_resource_id()
{
await context.Database.EnsureCreatedAsync();

context.Add(customer);
await context.AddAsync(customer);

await context.SaveChangesAsync();
}
Expand Down Expand Up @@ -1449,14 +1449,14 @@ public async Task Can_use_detached_entities_without_discriminators()
{
await context.Database.EnsureCreatedAsync();

context.Add(customer);
await context.AddAsync(customer);

await context.SaveChangesAsync();
}

using (var context = new NoDiscriminatorCustomerContext(options))
{
context.Add(customer).State = EntityState.Modified;
(await context.AddAsync(customer)).State = EntityState.Modified;

customer.Name = "Theon Greyjoy";

Expand All @@ -1470,7 +1470,7 @@ public async Task Can_use_detached_entities_without_discriminators()
Assert.Equal(42, customerFromStore.Id);
Assert.Equal("Theon Greyjoy", customerFromStore.Name);

context.Add(customer).State = EntityState.Deleted;
(await context.AddAsync(customer)).State = EntityState.Deleted;

await context.SaveChangesAsync();
}
Expand Down Expand Up @@ -1570,7 +1570,7 @@ public async Task Can_use_non_persisted_properties()
{
await context.Database.EnsureCreatedAsync();

context.Add(customer);
await context.AddAsync(customer);

await context.SaveChangesAsync();
Assert.Equal("Theon", customer.Name);
Expand Down Expand Up @@ -1609,7 +1609,7 @@ public async Task Add_update_delete_query_throws_if_no_container()
var customer = new Customer { Id = 42, Name = "Theon" };
using (var context = new CustomerContext(options))
{
context.Add(customer);
await context.AddAsync(customer);

Assert.StartsWith(
"Response status code does not indicate success: NotFound (404); Substatus: 0",
Expand All @@ -1618,7 +1618,7 @@ public async Task Add_update_delete_query_throws_if_no_container()

using (var context = new CustomerContext(options))
{
context.Add(customer).State = EntityState.Modified;
(await context.AddAsync(customer)).State = EntityState.Modified;

Assert.StartsWith(
"Response status code does not indicate success: NotFound (404); Substatus: 0",
Expand All @@ -1627,7 +1627,7 @@ public async Task Add_update_delete_query_throws_if_no_container()

using (var context = new CustomerContext(options))
{
context.Add(customer).State = EntityState.Deleted;
(await context.AddAsync(customer)).State = EntityState.Deleted;

Assert.StartsWith(
"Response status code does not indicate success: NotFound (404); Substatus: 0",
Expand All @@ -1653,7 +1653,7 @@ await Assert.ThrowsAnyAsync<Exception>(
{
await context.Database.EnsureCreatedAsync();
context.Add(new ConflictingIncompatibleId { id = 42 });
await context.AddAsync(new ConflictingIncompatibleId { id = 42 });
await context.SaveChangesAsync();
});
Expand Down Expand Up @@ -1688,7 +1688,7 @@ public async Task Can_add_update_delete_end_to_end_with_conflicting_id()
{
await context.Database.EnsureCreatedAsync();

context.Add(entity);
await context.AddAsync(entity);

await context.SaveChangesAsync();
}
Expand Down Expand Up @@ -1754,7 +1754,7 @@ public async Task Can_have_non_string_property_named_Discriminator()
using var context = new NonStringDiscriminatorContext(Fixture.CreateOptions());
context.Database.EnsureCreated();

context.Add(new NonStringDiscriminator { Id = 1 });
await context.AddAsync(new NonStringDiscriminator { Id = 1 });
await context.SaveChangesAsync();

Assert.NotNull(await context.Set<NonStringDiscriminator>().OrderBy(e => e.Id).FirstOrDefaultAsync());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public async Task Entities_with_null_PK_can_be_added_with_normal_use_of_DbContex

Assert.Null(item.Id);

var entry = context.Add(item);
var entry = await context.AddAsync(item);

var id = entry.Property("__id").CurrentValue;

Expand Down
Loading

0 comments on commit b02da05

Please sign in to comment.