A simple way is to use a value converter.
The code could look like this (using the answer from How to truncate milliseconds off of a .NET DateTime for the actual conversion logic):
public class YourContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<YourEntity>()
.Property(e => e.YourProperty)
.HasConversion(
v => v.AddTicks(-(dateTime.Ticks % TimeSpan.TicksPerSecond));,
v => v);
}
}
Of course you can also explicitly define and then reuse the value converter and you can also apply it to all properties of a certain type like 'DateTimeOffset', by iterating over the entities and their properties via ModelBuilder
.