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

date/time value generator + .net conversion #436

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Strandedpirate
Copy link

@Strandedpirate Strandedpirate commented Apr 8, 2023

.Net & EF don't handle UTC date/time columns correctly out of the box. By default all date/time objects are created in .Net using DateTimeKind.Local.

This request:

  • allows the definition of a global date/time value generator.
  • allows the definition of a global date/time kind.

EFG Configuration

mapping:
  namespace: "{Project.Namespace}.Domain.EntityConfiguration"
  directory: '{Project.Directory}\My.Domain\EntityConfiguration'
  document: false
  dateTimeKind: Utc # configures the conversion of date/time columns as UTC in .Net
  dateTimeDefaultValueGenerator: UtcValueGenerator # defines the default date/time value generator

Produces

builder.Property(t => t.CreatedOn)
    .IsRequired()
    .HasColumnName("CreatedOn")
    .HasColumnType("datetime2(1)")
    .HasDefaultValueSql("(sysutcdatetime())")
    .HasConversion(t => t, t => DateTime.SpecifyKind(t, DateTimeKind.Utc))
    .HasValueGenerator<UtcValueGenerator>();

Sample UtcValueGenerator

internal class UtcValueGenerator : ValueGenerator<DateTime>
{
    public override bool GeneratesTemporaryValues => false;

    public override DateTime Next([NotNullAttribute] EntityEntry entry)
    {
        return DateTime.UtcNow;
    }
}

@Strandedpirate
Copy link
Author

@pwelter34 what do you think about this pull request?

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

Successfully merging this pull request may close these issues.

1 participant