When inserting new rows the NotifiedAt
column with the [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
attribute doesn’t get auto-populated.
The example from the official docs:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public DateTime Inserted { get; set; }
What am I doing wrong?
Docs: https://learn.microsoft.com/en-us/ef/core/modeling/generated-properties?tabs=data-annotations#value-generated-on-add-1
Code:
[Index(nameof(OwnerId))]
[Index(nameof(UserPrincipalName))]
[Index(nameof(NotifiedAt))]
public class ApplicationNotification
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; init; }
[Required] public Guid OwnerId { get; set; }
[MaxLength(256)] public string? DisplayName { get; set; }
[MaxLength(256)] public string? GivenName { get; set; }
[MaxLength(256)] public string? Surname { get; set; }
[Required] [MaxLength(256)] public string? UserPrincipalName { get; set; }
[Required] public string? Message { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public DateTime NotifiedAt { get; set; }
Error:
[2024-09-24T08:36:04.683Z] INSERT INTO [ApplicationNotifications] ([Id], [DisplayName], [GivenName], [Message], [OwnerId], [Surname], [UserPrincipalName])
[2024-09-24T08:36:04.683Z] OUTPUT INSERTED.[NotifiedAt]
[2024-09-24T08:36:04.683Z] VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6);
[2024-09-24T08:36:04.702Z] Failed to dispatch notification to owner e122277b-f274-4f61-b12d-d9dac9c0cea4 for 1 applications
[2024-09-24T08:36:04.702Z] An exception occurred in the database while saving changes for context type 'LEGO.IAM.AssetGovernance.DbContexts.AssetOwnershipContext'.
[2024-09-24T08:36:04.702Z] Result: Failed to dispatch notification to owner e122277b-f274-4f61-b12d-d9dac9c0cea4 for 1 applications
[2024-09-24T08:36:04.702Z] Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
[2024-09-24T08:36:04.702Z] Exception: Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
[2024-09-24T08:36:04.702Z] ---> Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot insert the value NULL into column 'NotifiedAt', table 'AssetGovernance.dbo.ApplicationNotifications'; column does not allow nulls. INSERT fails.
[2024-09-24T08:36:04.702Z] ---> Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot insert the value NULL into column 'NotifiedAt', table 'AssetGovernance.dbo.ApplicationNotifications'; column does not allow nulls. INSERT fails.