-
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
Guid [DatabaseGenerated(DatabaseGeneratedOption.Identity)] #14518
Comments
Note for triage. This should not attempt to insert null. However, it's not clear to me whether modelBuilder
.Entity<Record>()
.Property(e => e.UniqueId)
.HasDefaultValueSql("newid()"); should be needed as well. On the one hand, this explicitly sets the property store-generated, which is necessary since there is no value converter. On the other hand, if |
If I add the modelbuilder code with "newid()" to get it working. But what is the point of using dataannotations and adding [DatabaseGenerated(DatabaseGeneratedOption.Identity)] to a property (which is not the [Key]) if doesn't work and doesn't create a new Guid on insert? |
@wstaelens This is a bug: it should treat the column as computed. But also see #14532 |
It actually is a bug. NewId() is used for identifiers. Identity in SQL Server has a different meaning (i.e. int column auto counting). The way I did that in the last years was
Alternatively actually handle it in code - the guid identity is one element that is nice to actually generate on the client and then use up to the server storage. That is sort of how it is meant to be used, the whole concept of a guid is that it CAN be generated on the client. |
if I use .HasDefaultValueSql("newid()"); , this is removed and returned back to .ValueGeneratedNever(); when scaffold command is run, what would be the correct approach to preserve .HasDefaultValueSql("newid()"); when Scafold is run in the future. |
Needs some more investigation. |
This is the command that we send INSERT INTO [Records] ([Info], [Title])
VALUES (@p0, @p1); Since the column is not setup to generate values SQL Server throws. |
also similar:
https://stackoverflow.com/questions/47152056/ef-core-unique-indexed-auto-generated-guid-column-that-is-not-the-primary-ke
I have an asp.net core mvc 2.2.1 website using EF code first.
Model:
In the controller in the Create method does not create and assign a new Guid, so i'm not manually adding Guids to the model on creation:
Create view:
The text was updated successfully, but these errors were encountered: