SQLite bigint 到 C# 中的日期时间

use*_*563 3 c# sqlite datetime

我在我的 UWP 项目中使用 SQLite。我的模型类具有 DateTime 字段:

public class EducationInfo
{
    [SQLite.Net.Attributes.PrimaryKey, SQLite.Net.Attributes.AutoIncrement]
    public int educationInfoId { get; set; }
    public String Education { get; set; }
    public bool Enabled { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int UserId { get; set; }

    // Not in the DB
    [Ignore]
    public string FormattedStartDate { get; set; }
    [Ignore]
    public string FormattedEndDate { get; set; }

    public EducationInfo()
    {}

    public EducationInfo(String edu, bool enabled, DateTime st, DateTime ed, int userId)
    {
        this.Education = edu;
        this.Enabled = enabled;
        this.StartDate = st;
        this.EndDate = ed;
        this.UserId = userId;
    }

}
Run Code Online (Sandbox Code Playgroud)

问题是 SQLite 将 DateTime 字段创建为 bigint。如何读取这个 bigint 并将其设置为 DateTime?

`

var education = connection.Table<EducationInfo>().Where(e => e.UserId == user.userID);
                        foreach(var edu in education)
                        {
                            EducationInfo educationInfo = new EducationInfo();
                            educationInfo.StartDate = edu.StartDate;
                        }
Run Code Online (Sandbox Code Playgroud)

上面的代码没有返回错误,但日期值是错误的。`

sac*_*hin 5

这是存储为 DateTime 而不是 bigint的解决方案:

SQLiteConnection 构造函数采用默认为 true 的参数 storeDateTimeAsTicks。如果您希望它使用 DateTime 类型而不是 bigint,则可以将其传递为 false。

https://github.com/oysteinkrog/SQLite.Net-PCL/issues/203