docs icon indicating copy to clipboard operation
docs copied to clipboard

Is the section on the hi/lo algorithm for EF Core accurate?

Open Beachj opened this issue 7 years ago • 4 comments

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.

Beachj avatar Oct 25 '19 14:10 Beachj

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.

mvelosop avatar Nov 05 '19 22:11 mvelosop

Closing this issue now but feel free to comment, will reopen if needed.

mvelosop avatar Nov 06 '19 21:11 mvelosop

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

Beachj avatar Nov 07 '19 22:11 Beachj

Glad this could help you!

Yep UseHiLo is the new API, will update that in the upcoming .NET Core 3 migration.

Cheers.

mvelosop avatar Nov 08 '19 12:11 mvelosop