PetaPoco / SQLite聚合日期错误的解决方法

时间:2013-11-06 02:32:59

标签: sqlite petapoco

这是创建SQLite数据库的完整代码,将一些数据填充到表中然后尝试检索它。如果在datetime列周围有一个聚合函数,PetaPoco将抛出一个错误。

using System;
using PetaPoco;

class Program
{
    static void Main(string[] args)
    {
        bool filenew = false;
        if (!System.IO.File.Exists(@"c:\temp\database.sq3"))
            filenew = true;
        System.Data.SQLite.SQLiteConnection sqltc = new System.Data.SQLite.SQLiteConnection("Data Source=" + @"c:\temp\database.sq3");
        sqltc.Open();
        PetaPoco.Database db = new Database(sqltc);
        if (filenew)
            db.Execute("create table test1 (ID_CHANNEL integer primary key autoincrement, dtfld DateTime null, name string)");
        test1 t = new test1();
        t.name = "No Date";
        db.Insert(t);
        t = new test1();
        t.dtfld = DateTime.Now;
        t.name = "with date";
        db.Insert(t);
// SUCCESS:
        test1 lt1 = db.First<test1>("select dtfld from test1 where ID_Channel = 2");
// FAILURE:
        test1 lt2 = db.First<test1>("select max(dtfld) as dtfld from test1 where dtfld is not null");
    }

    [PetaPoco.TableName("test1")]
    [PetaPoco.PrimaryKey("ID_Channel")]
    public class test1
    {
        public long ID_Channel { get; set; }
        public DateTime? dtfld { get; set; }
        public string name { get; set; }
    }
}

任何人都可以建议修复仍然意味着POCO对象包含日期时间,我仍然可以访问日期的最大值吗?

2 个答案:

答案 0 :(得分:4)

找到解决方案 - 切换到NPoco。上面唯一的变化是将“PetaPoco”替换为“NPoco”。

答案 1 :(得分:0)

在PetaPoco.cs中更改private static Func<object, object> GetConverter功能。替换语句return Convert.ChangeType(src, dstType, null);

TypeConverter conv = TypeDescriptor.GetConverter(dstType);
return conv.ConvertFrom(src);