I have a scenario where my model has the fields CreatedOn and UpdatedOn and following what I found in the documentation of EF Core I implemented my class as follows
builder.Entity<Registro>(b =>
{
b.Property<DateTime>("CreatedOn")
.IsRequired()
.ValueGeneratedOnAdd()
.HasDefaultValueSql("GETUTCDATE()");
b.Property<DateTime>("UpdatedOn")
.IsRequired()
.ValueGeneratedOnAddOrUpdate()
.HasDefaultValueSql("GETUTCDATE()"); ;
});
public class Registro
{
public Guid Id { get; set; }
public string Nome { get; set; }
}
On insert both fields are saved with the correct value, however the update it does not happen as I expected, the UpdatedOn keeps the save value. It also follows how I am doing the entity update
public void Put(Guid id, [FromBody]string value)
{
var r = context.Registros.SingleOrDefault(x => x.Id == id);
r.Nome = value;
context.SaveChanges();
}