Skip to content

Unspecified DateTime Kind F# #3776

Description

@Mrjuanblack

Describe your issue

I'm encountering an issue when dealing with DateTime objects. Seems like DateTime objects being inserted into the db will always have an Unspecified Kind, and I'm using Postgres so that will throw an error.
I'm making sure that I'm using UTC now to prevent that, but the error persists.
To make it work (for now) I'm falling back to EF Core methods, which makes me think that this might be a Linq2Db issue.

Cannot write DateTime with Kind=Unspecified to PostgreSQL type 'timestamp with time zone', only UTC is supported. Note that it's not possible to mix DateTimes with different Kinds in an array/range. See the Npgsql.EnableLegacyTimestampBehavior AppContext switch to enable legacy behavior.

Steps to reproduce

Model

[<CLIMutable>]
type AppointmentCalendarEvent = {
    [<Key>]
    [<DatabaseGenerated(DatabaseGeneratedOption.Identity)>]
    Id : int
    AdiId : int
    StartTime : System.DateTime
    EndTime : System.DateTime
}

Repository method

type Repository(context : AppDbContext) =
    member _.insertAppointment() =
        let event : AppointmentCalendarEvent = {
            Id = 0 // For inserts with auto-generated PK
            AdiId = 0
            StartTime = System.DateTime.UtcNow
            EndTime = System.DateTime.UtcNow
        }

        task {
            try
                let! eventId = context.CreateLinqToDbContext().InsertWithInt32IdentityAsync(event)
                return event |> Result.Ok
            with
            | e ->
                return Result.Error e
        } |> Async.AwaitTask

Context

type AppDbContext(options: DbContextOptions<AppDbContext>) =
    inherit DbContext(options)

    [<DefaultValue>] val mutable appointmentCalendarEvent : DbSet<AppointmentCalendarEvent>
    member this.AppointmentCalendarEvent
        with get() = this.appointmentCalendarEvent
        and set v = this.appointmentCalendarEvent <- v

    override _.OnModelCreating builder =
        builder.RegisterOptionTypes() // enables option values for all entities
    
type TestDbContextFactory() =
    member this.CreateDbContext() =
        let options = new DbContextOptionsBuilder<AppDbContext>()
        options.UseNpgsql("connectionStringHere").UseFSharpTypes() |> ignore
        new AppDbContext(options.Options)

I'll link the project I created for this issue: https://github.com/Mrjuanblack/testProject

Environment details

Linq To DB version: 4.2.0

Database (with version): Postgres 14.3

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: has-prThere is active PR for issue

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions