I have used:
modelBuilder.Entity<PersonSurveyed>()
.Property(p => p.Created)
.HasDefaultValueSql("getdate()")
.ValueGeneratedOnAdd();
modelBuilder.Entity<PersonSurveyed>()
.Property(p => p.LastUpdated)
.HasDefaultValueSql("getdate()")
.ValueGeneratedOnAdd();
in OnModelCreating to generate defaults for properties when they are created. This works fine on create but when I save the records values created with these defaults get set to null. This is not happneing database side because when I go and do these transaction manually the values are left alone.
public class EntityBase
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Display(Name = "Created")]
[DisplayFormat(DataFormatString = "{0:dd MMM yyyy h:m:s tt}")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public DateTime Created { get; }
[Display(Name = "Last Updated")]
[DisplayFormat(DataFormatString = "{0:dd MMM yyyy h:m:s tt}")]
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime LastUpdated { get; }
}
As you can see there is no set so the properties shouldn't be being written to when I save changes.
Any help would be very welcome.
Thanks