Closed don-flamingo closed 1 year ago
This is currently by-design. If async value generation is needed, then AddAsync
must be used to track the entity. This is because, in the general case, an entity can be tracked in many non-async contexts, most notably when DetectChanges
is called implicitly by a non-async method.
Note for triage: with HiLo becoming more useful since EF Core 7, async value gen is likely to get more use. While the general case can't be changed, we could re-introduce DetectChangesAsync
, which could in turn be called from SaveChangesAsync
.
One additional radical/spicy thought: we could also move value generation to SaveChanges-time, rather than doing it at Add-time. The main advantage here is that we'd be able to obsolete AddAsync, which seems to cause quite a bit of confusion (and is indeed rarely needed). This would also align client-side value generation to happen at the same time as database-generated, which would make our behavior a bit more consistent.
Of course, this would mean users need to wait until SaveChanges before they can copy out the generated property (again, just like database-generated). This is related to #30753, where we may want to switch to server-generated GUIDs by default (it represents the same minor breaking change).
@roji One of the original motivations for Hi/Lo was that it generated real IDs as soon as the entity is tracked--same reason for client-side GUIDs. EF6 could never do this, and it was a frequent complaint. We could create a HiLo generator which has the characteristics that you like while not generating IDs until SaveChanges. (i.e. one extra round trip ever x number of calls to SaveChanges.) That would be much simpler in lots of places, but wouldn't match one of the main reasons for doing it in the first place.
You're saying that many people complained about not being able to get the real ID before calling SaveChanges? That strikes me as a bit odd, but in that case yeah, the current behavior makes more sense.
Duplicate of #30957
File a bug
When I have added an entity by the inner relation, without invoking the AddAsync method. After SaveChangesAsync my attached ValueGenerator doesn't use the override NextAsync method to generate async value, instead of it uses sync method.
Include your code
Include stack traces
NONE
Include verbose output
it's caused by:
Include provider and version information
EF Core version: Database provider: Npgsql.EntityFrameworkCore.PostgreSQL Target framework: .NET 6.0 / EF 6.0.6 Operating system: Mac os