c# - 如何在 PostgreSQL 中存储 DateTimeOffset

标签 c# entity-framework postgresql datetimeoffset

我在使用 Entity Framework 从 PostgreSQL 数据库中检索 DateTimeOffset 时遇到问题。至于研究问题,我发现 this article这有助于我了解问题所在,但我不知道如何解决。

我有一个 API 允许用户上传文件(主要是图像),它提取图像的拍摄日期并将其存储在数据库中。它大部分时间都很好用。但是,如果日期在 2007 年之前的 3 月 11 日到 4 月的某个时间(日期因年份而异),它可以很好地保存到数据库中,但是在尝试检索该行时,它会抛出错误:

The UTC Offset of the local dateTime parameter does not match the offset argument.

我在 postgreSQL 中为 CameraDate 字段使用 Timestamp With Time Zone 类型。我不知道如何让它正常工作。如果有一种方法可以在不更改数据库的情况下执行此操作,那将是更可取的。

例子:

2001-04-01 10:47:17-06 Works
2001-03-01 10:47:17-06 Works
2001-03-13 10:47:17-06 Doesn't work
2007-03-13 10:47:17-06 Works

这是我的代码:

Context.Files.Add(file);
Context.Entry(file).Reload();

文件类如下所示(为简洁起见删除了一些字段):

public class File
{
    [Column("FileId")]
    public override Guid ID { get; set; }
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int DisplayId { get; set; }
    public DateTimeOffset? CameraDate { get; set; }
}

最佳答案

好的,这似乎是 npgsql 或 npgsql.EntityFramework 中 DateTimeOffset 的错误。似乎记录了其他几个问题 in their issue tracker .特别是issue #542非常接近 - 但有不同的异常消息。

我建议您在那里创建一个新问题,因为它们最有助于进一步诊断。

关于c# - 如何在 PostgreSQL 中存储 DateTimeOffset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29127128/

相关文章:

c# - 如何更改 WPF 进度条上的颜色

c# - 如何链接到 WPF 菜单项内的网页?

c# - Entity Framework - 加载带有子实体的实体

c# - 异常类型 'System.ObjectDisposedException'

sql - postgresql 中的 Crosstab() 函数导致错误。 (已安装 tablefunc 模块)

ruby-on-rails - rails 3 : Sort a ActiveRelation by Ratings Counted from another table

c# - 如何更改 DataGrid 的列宽

c# - 从数据表导出npoi

c# - 如何在查询语法中编写 Take(1)

sql-server - 从 Postgres 迁移到 SQL Server 2008