Workaround for PetaPoco / SQLite Aggregation Date Error

Here is the complete code for creating the SQLite database, fill in some data in the table and then try to restore it. If there is an aggregate function in the datetime column, PetaPoco throws 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 someone suggest a fix that still means that the POCO object contains the date and time and I can still get the maximum date?

+3
source share
2 answers

Solution Found - Switch to NPoco. The only change above was to replace "PetaPoco" with "NPoco".

+4
source

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

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

Source: https://habr.com/ru/post/957066/


All Articles