Pregunta

Here's complete code to create a SQLite database, fill some data into a table and then try to retrieve it. If there's an aggregate function around a datetime column, PetaPoco will throw an error.

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; }
    }
}

Can anybody suggest a fix that still means the POCO object contains a datetime, and I can still access the max of a date?

¿Fue útil?

Solución

Found a solution - switch to NPoco. The only change to above was to replace "PetaPoco" with "NPoco".

Otros consejos

In PetaPoco.cs change the private static Func<object, object> GetConverter function. Replace the statement return Convert.ChangeType(src, dstType, null);

TypeConverter conv = TypeDescriptor.GetConverter(dstType);
return conv.ConvertFrom(src);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top