-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
EFCore 7 ValueGeneratedOnUpdate not working on Guid that is not a key #29752
Comments
This is the expected behavior, see #28024 (comment). Note also the following sentence in our docs:
But I've found several places in the docs which are wrong, will fix.
If by "smart order" you mean SQL Server sequential IDs (for optimizing indexes over them), then you can use SQL Server's NEWSEQUENTIALID. Alternatively, you can tell EF to generate the sequential GUIDs on the client side, which is what it does for GUID keys. This is more efficient when the generated GUID is a key (since it may be referenced by a dependent, affecting batching in insertions), but should be the same for non-keys: modelBuilder.Entity<Blog>().Property(b => b.Guid).HasValueGenerator(typeof(SequentialGuidValueGenerator)); |
Duplicate of #28024 |
Docs PR: dotnet/EntityFramework.Docs#4172 |
Thank you for the answer roji! I confirm the fix, i commented And added this: The guid's are created on add and they look sequential! For the sake of improving documentation, what confused me from this documentation link is: For keys it specifically sais EF takes care of things.
Here it says that we can also do it for non-key, which is what we want:
So above as far as i understand it sais that for GUID it works automatically, but for DateTime something else needs to be done. My 2 cents on Arthur Vickers response, though to be clear i don't claim to have experience with this:
If i add the option ValueGeneratedOnAdd, talking semantically, i expect a value to be generated on add :) Thanks for the answer! |
Yes, that's the incorrect part of the docs which I'm fixing in #4172. Thanks also for your feedback - we'll discuss this. |
Design decision: we're not going to make any change here. The main reason is that starting to do client-side generation of GUIDs would be a breaking change; if anyone is using ValueGeneratedOnAdd to indicate that a value is being generated by the database (e.g. via a default value), EF will start overriding that by sending client-generated values. |
We use a long Id as primary key and added a Guid so we can find related data cross databases/microservices.
public class User
{
public long Id { get; set; }
public Guid Username { get; set; }
public string PhoneNumber { get; set; }
....
}
And in the DBContext OnModelCreating we have
modelBuilder.Entity()
.Property(e => e.Username)
.ValueGeneratedOnAdd();
The problem is initializing the db fails with SqlException: Cannot insert the value NULL into column 'Username'
Basically the Username is not autogenerated on add.
Adding .HasDefaultValueSql("NEWID()"); in OnModelCreating is a fake solve, it basically works without ValueGeneratedOnAdd but the id's are random, they have no smart order.
The only way to make it work was to add .HasAlternateKey(e => e.Username) in OnModelCreating.
With this we get nice generated guid's that are in some order so it's probably faster to search them.
Note: we are not using Username as an actual key for a relation inside the table, we use it to find the related entity in a different microservice&database. So we just needed a unique id generated on add, ideally optimized for searching on it.
Expected Behavior
Expected behaviour imo would be to have everything working with just ValueGeneratedOnAdd and without HasAlternateKey. There is no such mention (that i understand) in the documentation
.NET Version
7.0.100
The text was updated successfully, but these errors were encountered: