Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using DateTimeOffset.Now in ExecuteUpdateAsync doesn't take column precision into account #1930

Open
Archomeda opened this issue Jun 28, 2024 · 0 comments

Comments

@Archomeda
Copy link

Steps to reproduce

Have a simple database table with a datetime(6) column

CREATE TABLE `t` (
    `Date` datetime(6)
)

with an associated model

public class TableModel
{
    public DateTimeOffset Date { get; set; }
}

and configured in the context as

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder
        .Entity<TableModel>()
        .ToTable("t")
        .Property(x => x.Date).HasPrecision(6);
}

When this is set up, insert a single item with just whatever date value.

Then, update the value with the current timestamp value from the database (instead of C#) as follows:

await dbContext.Table
    .ExecuteUpdateAsync(x => x
        .SetProperty(x => x.Date, x => DateTimeOffset.Now));

The issue

It correctly assumes it needs to take UTC_TIMESTAMP.
However, it doesn't take the precision into account, which is configured as 6.
So the actual query translates to something like this:

UPDATE `t` AS `t`
SET `t`.`Date` = UTC_TIMESTAMP()

It's crucial in my setup that I update the row with the current timestamp from a single source with this precision, as my application may run on multiple instances that may run with a slightly desynced clock.
I cannot use any configuration that always updates the column on save, as I only need to update this column sometimes when changing other columns.
If there's any other setup that will already work for my case, I'm all ears as well.
Right now I'm resorting to ExecuteSqlInterpolatedAsync, which works, but is far from ideal.

Further technical details

MySQL version: 11.3.2-MariaDB-1:11.3.2+maria~ubu2204
Operating system: Windows 10 / Docker via WSL2
Pomelo.EntityFrameworkCore.MySql version: 8.0.2
Microsoft.AspNetCore.App version: 8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant