【发布时间】:2016-01-11 18:38:21
【问题描述】:
在我们必须使用的数据库(即 DB2)中,有些字段存储为字符,但实际上是其他对象,最常见的是底层应用程序存储日期和时间的自定义方式。例如:
[Table]
public class ExampleTable {
// This is stored in the DB as a char in the format: 2016-01-11-11.39.53.492000
[Column(Name = "WTIMESTAMP")] public string WriteTimestamp { get; set; }
}
有没有办法告诉 linq2db 在从数据库转换到/从数据库时使用的转换方法,这也将允许我们将这些属性作为我们想要的对象(例如,C# DateTime 对象)来访问,但是得到以正确的格式保存回来?
我想到的一件事是这样的:
[Table]
public class ExampleTable {
public DateTime WriteTimestamp { get; set; }
// This is stored in the DB as a char in the format: 2016-01-11-11.39.53.492000
[Column(Name = "WTIMESTAMP")] public string WriteTimestampRaw
{
get {
return ConvertWriteTimestampToDb2Format(WriteTimestamp);
}
set {
WriteTimestamp = ConvertWriteTimestampToDateTime(value);
}
}
}
然后我们访问 WriteTimestamp,但 linq2db 在查询中使用 WriteTimestampRaw。
但是,我不确定这是最好的选择还是唯一的选择。提前致谢。
【问题讨论】:
-
DB2 中的实际数据类型是
(VAR)CHAR吗?如果它是TIMESTAMP类型,如果您使用 DateTime 数据类型,.net 应该能够对其进行转换。 -
@bhamby 不幸的是,它是 CCID 为 37 的字符类型。还有其他几个类似的东西。例如,在另一个地方,它们只存储儒略日期格式的日期(YYYYDDD,其中 DDD 是一年中的某一天),实际上是以十进制格式存储的。
-
inq2db 3.0.0-rc1 计划于本周发布,增加了使用值转换器 github.com/linq2db/linq2db/wiki/… 的每列转换配置支持