I'm using SQLite in my UWP project. My model class has DateTime fields:
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;
}
}
The problem is that SQLite create the DateTime fields as bigint. How can I read this bigint and set as 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;
}
The code above doesn't return error, but the date value is wrong. `
Here is a solution to store as DateTime instead of bigint:
The SQLiteConnection constructor takes a parameter storeDateTimeAsTicks which defaults to true. You can pass that as false if you want it to use a DateTime type instead of bigint.
https://github.com/oysteinkrog/SQLite.Net-PCL/issues/203
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With