在我们必须使用的数据库(即 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。
但是,我不确定这是最好的还是唯一的选择。提前致谢。