Is the section on the hi/lo algorithm for EF Core accurate?
The section about "Hi/Lo" in ef core states:
The Hi/Lo algorithm is useful when you need unique keys before committing changes. As a summary, the Hi-Lo algorithm assigns unique identifiers to table rows while not depending on storing the row in the database immediately. This lets you start using the identifiers right away, as happens with regular sequential database IDs.
However after using the same implementation, it seems that this doesn't actually allow you to access the key before committing a transaction (similar to other auto-generation techniques), in this case calling SaveChanges.
How would one obtain an incremented key when the object is created, if for example a domain event handler needs that information?
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 9c1c070c-b2ff-f29d-21df-bcc7bf9dd39d
- Version Independent ID: 975107e9-0fc2-b867-d917-595412cbe422
- Content: Implementing the infrastructure persistence layer with Entity Framework Core
- Content Source: docs/architecture/microservices/microservice-ddd-cqrs-patterns/infrastructure-persistence-layer-implemenation-entity-framework-core.md
- Product: dotnet-architecture
- Technology: microservices
- GitHub Login: @nishanil
- Microsoft Alias: nanil
Hi @Beachj, thanks for your feedback.
It's not actually shown in that article but it's on the eShopOnContainers code.
However, since it's not easy to see that in eShopOnContainers, I set up a small console app to showcase the correct way to use it, with as less clutter as possible.
You can see the app in this repo: https://github.com/mvelosop/explore-hi-lo-algorithm
The code is in .NET Core 3.0 with EF Core 3.0 (of course 😉) but it works just the same in 2.x
Hope this helps.
Closing this issue now but feel free to comment, will reopen if needed.
Thanks for the update - I was missing the modelBuilder.HasSequence(...) piece in the entity configuration. My app was recently migrated to 3.0 and I wanted to avoid using the deprecated .ForSqlServerUseSequenceHiLo call that eshop uses. Figured just using UseHilo() would be enough - so I guess just some confusion with the docs in relation to 3.0 with what configuration calls need to be made
Glad this could help you!
Yep UseHiLo is the new API, will update that in the upcoming .NET Core 3 migration.
Cheers.