mardi 8 mars 2016

SQLite.Net-PLC OrderBy function returns Null reference exception

Platform:

Working

I had a Table

public class CommandData
{
    [PrimaryKey, AutoIncrement]
    [Column("Id")]
    public Guid Id { get; set; }

    [Column("TimeStamp")]
    public DateTime TimeStamp { get; set; }
}

Breaking change

But, SQLite returns the DateTime's Kind as Unspecified I had to change the table to always return UTC Time (I also tried DateTime.SpecifyKind(this.TimeStampStore , DateTimeKind.Utc), where TimeStampStore is type DateTime) :

public class CommandData
{
    [PrimaryKey, AutoIncrement]
    [Column("Id")]
    public Guid Id { get; set; }

    [SQLite.Ignore]
    public DateTime TimeStamp
    {
        get
        {
            return DateTime.Parse(this.TimeStampStore).ToUniversalTime();
        }
        set
        {
            TimeStampStore = value.ToUniversalTime().ToString("s") + "Z";
        }
    }

    [Column("TimeStamp")]
    public string TimeStampStore { get; set; }
}

This change broke the following:

SQLite.SQLiteConnection.Table<CommandData>().OrderBy(x=>x.TimeStamp) 

Stack trace:

at SQLite.TableQuery1.AddOrderBy[U](Expression1 orderExpr, Boolean asc)

at SQLite.TableQuery1.OrderBy[U](Expression1 orderExpr)

Reason

After a bit on investigation, I found that the reason for the System.NullReferenceException is the dot operator on the Table.FindColumnWithPropertyName(mem.Member.Name) in SQLite.cs

 private TableQuery<T> AddOrderBy<U> (Expression<Func<T, U>> orderExpr, bool asc)
 {
     ...
                var q = Clone<T> ();
                if (q._orderBys == null) {
                    q._orderBys = new List<Ordering> ();
                }
                q._orderBys.Add (new Ordering {
                    ColumnName = Table.FindColumnWithPropertyName(mem.Member.Name).Name,
                    Ascending = asc
                });
                return q;
 }

 public Column FindColumnWithPropertyName (string propertyName)
 {
     var exact = Columns.FirstOrDefault (c => c.PropertyName == propertyName);
     return exact;
 }

The propertyName is "TimeStamp", but

Columns[].PropertyName is "TimeStampStore"

Columns[].Name is "TimeStamp"

and and thus returns a null

If I change c => c.PropertyName == propertyName to c => c.Name == propertyName then my code works.

Question

Am I doing something wrong with my setup? I can read and write perfectly but this one thing is giving me quite a headache.

Thank you.

Aucun commentaire:

Enregistrer un commentaire