Skip to content

Issue with Timestamp mapping to DateTimeOffset EF Core #369

@DuelingCats

Description

@DuelingCats

I am testing time and date code and encountered an issue when trying to fetch TIMESTAMP WITH LOCAL TIMEZONE oracle column type to DateTimeOffset CLR type. I am working with a database first approach and according to the Oracle documentation, the TIMESTAMP WITH LOCAL TIMEZONE should be mapping to DateTimeOffset. When testing inserting data, it works as expected.

When fetching data at context.TimeTesting.ToList() below, I get an InvalidCastException that looks to be a problem with how DateTimeOffset is fetched. This is using Oracle.EntityFrameworkCore version 7.21.13 package. This might also be related to issue #193

image

Sample Code:

CREATE TABLE TIME_TESTING
(
  ID NUMBER GENERATED ALWAYS AS IDENTITY NOT NULL
, TIME_LOCAL TIMESTAMP(7) WITH LOCAL TIME ZONE
, DATE_TYPE DATE
, CONSTRAINT TIME_TESTING_PK PRIMARY KEY
  (
    ID
  )
  ENABLE
);

[Table("TIME_TESTING")]
public class TimeTesting
{
    [Key]
    [Column("ID")]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; } 

    [Column("TIME_LOCAL")]
    public DateTimeOffset TimeLocal { get; set; }

    [Column("DATE_TYPE")]
    public DateTime DateType { get; set; }
}
string dateMarchTest = "2024-03-10T06:59:48.734Z";
var dateMarchOffset = DateTimeOffset.Parse(dateMarchTest);

var dbContextFactory = _serviceProvider.GetService<IDbContextFactory<SMADbContext>>();
using (var context = dbContextFactory!.CreateDbContext())
{
    TimeTesting marchTest = new TimeTesting()
    {
        TimeLocal = dateMarchOffset,
        DateType = dateMarchOffset.LocalDateTime
    };

    context.Add(marchTest);
    context.SaveChanges();
}


using (var context = dbContextFactory!.CreateDbContext())
{
    var test = context.TimeTesting.ToList();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions