When I'm saving changes to the database, I'm running into the following exception:
Cannot insert the value NULL into column 'Registered', table
'EIT.Enterprise.KMS.dbo.LicenseEntry'; column does not allow nulls.
INSERT fails. The statement has been terminated.
The related code first model property looks like this:
[DatabaseGenerated(DatabaseGeneratedOption.Identity), DataMember]
public DateTime Registered { get; private set; }
... and here's why I'm confused: As far as I know, by providing the annotation [DatabaseGenerated(DatabaseGeneratedOption.Identity)
I'm ordering Entity Framework to auto-generate the field (in this case: Only once at creation time.)
So I'm expecting to have a non-nullable (required) field, without the possibility to alter the field manually where EF is taking care of.
What am I doing wrong?
Notes:
defaultValueSql
is also not an option, because I need to rely database independed for this project (e.g. for GETDATE()
).Try this:
[DatabaseGenerated(DatabaseGeneratedOption.Identity), DataMember]
public DateTime? Registered { get; private set; }
The question mark makes the property nullable
There is no default DateTime. Only a min value.
You could potentially just set the datetime on that entity before calling dbContext.SaveChanges
.
See this for a solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With