I have a code that automatically completes creation time and last updated time. When creating an entry the dates are inserted in both places, but when updating the code does not update LastUpdatedDate. The code is taken from the following website: docs.microsoft
public class ReservationModel
{
public int Id { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime? CreatedDate = DateTime.Now;
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime? LastUpdateDate = DateTime.Now;
}
DB:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ReservationModel>()
.Property(b => b.CreatedDate)
.HasDefaultValueSql("getdate()");
modelBuilder.Entity<ReservationModel>()
.Property(b => b.LastUpdateDate)
.ValueGeneratedOnAddOrUpdate()
.HasDefaultValueSql("getdate()");
}
Why is that and how to fix it?