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

Clarification for GUID generation behavior in EF Core 5.0 for SQL Server #2947

Closed
felinepc opened this issue Dec 14, 2020 · 4 comments · Fixed by #2998
Closed

Clarification for GUID generation behavior in EF Core 5.0 for SQL Server #2947

felinepc opened this issue Dec 14, 2020 · 4 comments · Fixed by #2998
Assignees
Milestone

Comments

@felinepc
Copy link

Ask a question

If a developer's goal is to have “sequential” GUID primary keys generated for faster performance, either by EF Core or the SQL Server’s NEWSEQUENTIALID, what is the expected behavior in EF Core 5.0 and what is the best practice?

This documentation says:

(SequentialGuidValueGenerator Class) Generates sequential Guid values optimized for use in Microsoft SQL server clustered keys or indexes, yielding better performance than random values. This is the default generator for SQL Server Guid columns which are set to be generated on add."

Does this mean if we don’t configure anything on a GUID property at all, EF Core 5 will automatically use its optimized sequential GUID generator to generate a non-temporary sequential GUID that will be saved to the DB? Or do we have to specify ValueGeneratedOnAdd?

But then there is this other documentation which says something different:

How the value is generated for added entities will depend on the database provider being used. Database providers may automatically set up value generation for some property types, but others may require you to manually set up how the value is generated.

For example, when using SQL Server, values will be automatically generated for GUID properties (using the SQL Server sequential GUID algorithm)…

This seems to contradict the first link, which said the GUIDs would be generated on client side.

Some clarifications on this behavior and best practice would be appreciated, since we’d often opt to use GUID instead of int for primary keys to avoid giving away business information to end users.

It would also be nice to know the best approach to take when we do want non-sequential, random GUIDs for properties that are security-sensitive.

Thank you!

Include provider and version information

EF Core version: 5.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 5.0
Operating system: Windows 10 Pro
IDE: Visual Studio 2019 16.8.3

@ajcvickers
Copy link
Member

@felinepc This statement:

For example, when using SQL Server, values will be automatically generated for GUID properties (using the SQL Server sequential GUID algorithm)

is intended to indicate that SequentialGuidValueGenerator is used. However, I can see that this could be interpreted as you did, so we could make this clearer.

@ajcvickers ajcvickers transferred this issue from dotnet/efcore Dec 15, 2020
@felinepc
Copy link
Author

SequentialGuidValueGenerator

Thanks. Just to clarify, is SequentialGuidValueGenerator now the default GUID generator for EF Core 5.0 entity class GUID key properties by convention? Asking this because EF Core Github seems to contain both GuidValueGenerator and SequentialGuidValueGenerator, but it's unclear which one is used.

@ajcvickers
Copy link
Member

SequentialGuidValueGenerator is the default when using the SQL Server provider. This hasn't changed--it's been this way since 1.0.

@roji
Copy link
Member

roji commented Dec 15, 2020

Yeah, the original intention was to say that the SQL Server algorithm would be used (but on the client-side).

Since then, investigation in dotnet/efcore#19124 (which contains lots of other relevant info) has shown that EF Core isn't actually implementing the SQL Server algorithm, though it's still generating optimal sequential GUIDs. We should indeed update.

@ajcvickers ajcvickers added this to the Backlog milestone Dec 18, 2020
roji added a commit that referenced this issue Jan 10, 2021
roji added a commit that referenced this issue Jan 10, 2021
roji added a commit that referenced this issue Jan 10, 2021
roji added a commit that referenced this issue Jan 13, 2021
roji added a commit that referenced this issue Jan 13, 2021
* Improvements to the value generation page
* Create new page on SQL Server value generation, with information
  on IDENTITY, seed/increment, etc.
* Delete "explicit values for generated properties" page, moving
  relevant content to the new SQL Server page and to the fixed-up
  generated properties page.

Fixes #2980
Fixes #2947
Closes #2999

Co-authored-by: Andriy Svyryd <AndriySvyryd@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants