0

I have a problem trying to get records from a Sqlite table using Linq2db+SQLite, here is my table below

CREATE TABLE LogEntry 
(
Time DATETIME NOT NULL ON CONFLICT ROLLBACK DEFAULT (CURRENT_TIMESTAMP), 
Reference STRING NOT NULL ON CONFLICT ROLLBACK, 
Team STRING, 
Operator STRING, 
ETMachine STRING, 
CheckPoint BOOLEAN DEFAULT (0) NOT NULL
);

down is C# code:

using (var db = new DataConnection())
  {
        var newItem = new LogEntry()
        {
          CheckPoint = false,
          ETMachine = "232323", // <= cause
          Operator = "asdasd",
          Reference = "asasas",
          Team = "wewe",
          Time = DateTime.Now
        };

        db.Insert<LogEntry>(newItem);

        foreach (var item in db.LogEntries) //<= error occurs here
        {
          MessageBox.Show(string.Format("{0}:{1}", item.ID, item.Time));
        }
}

and entity

    [Table("LogEntry")]
    public partial class LogEntry
  {
    [Column(Name = "ROWID"), NotNull, PrimaryKey, Identity] public int ID { get; set; } // datetime
    [Column, NotNull    ] public DateTime Time       { get; set; } // datetime
        [Column, NotNull    ] public string   Reference  { get; set; } // string(max)
        [Column,    Nullable] public string   Team       { get; set; } // string(max)
        [Column,    Nullable] public string   Operator   { get; set; } // string(max)
        [Column,    Nullable] public string   ETMachine  { get; set; } // string(max)
        [Column, NotNull    ] public bool     CheckPoint { get; set; } // boolean
    }

I get

'System.InvalidCastException' occurred in System.Data.SQLite.dll
Additional information: Specified cast is not valid.
@ System.Data.SQLite.SQLiteDataReader.VerifyType(Int32 i, DbType typ)
@ System.Data.SQLite.SQLiteDataReader.GetString(Int32 i)
@ lambda_method(Closure , IDataReader )
@ LinqToDB.Expressions.ConvertFromDataReaderExpression.ColumnReader.GetValue(IDataReader dataReader) dans i:\linq2db\Source\Expressions\ConvertFromDataReaderExpression.cs:ligne 128
@ lambda_method(Closure , QueryContext , IDataContext , IDataReader , Expression , Object[] )
@ LinqToDB.Linq.Query`1.<Map>d__6a.MoveNext() dans i:\linq2db\Source\Linq\Query.cs:ligne 1218
@ Peel.FrmMain.Testdb2() dans C:\Users\Administrateur\Documents\Work\Projects\PeeL\src\Peel\FrmMain.cs:ligne 67

I searched a little bit and found out that the issue rises when a string field of the table have numeric values ex. ETMachine property above, although the value is passed as string literal. Here, if I change the 232323 to 232a323 it will work fine.

My question, How can I force Linq2db to insert the value as string and not numeric? Or, How can I force the Linq2DB to get values as its appropriate field type?

BTW target framework is 4.0.

1 Answers1

3

I finally found the problem, the issue was in column datatype STRING, after I changed it to TEXT it worked fine.

According to SQLite Documentation...

2.1 Determination Of Column Affinity

The affinity of a column is determined by the declared type of the column, according to the following rules in the order shown:

  • If the declared type contains the string "INT" then it is assigned INTEGER affinity.
  • If the declared type of the column contains any of the strings "CHAR", "CLOB", or "TEXT" then that column has TEXT affinity. Notice that the type VARCHAR contains the string "CHAR" and is thus assigned TEXT affinity.
  • If the declared type for a column contains the string "BLOB" or if no type is specified then the column has affinity BLOB.
  • If the declared type for a column contains any of the strings "REAL", "FLOA", or "DOUB" then the column has REAL affinity.
  • Otherwise, the affinity is NUMERIC. Note that the order of the rules for determining column affinity is important. A column whose declared type is "CHARINT" will match both rules 1 and 2 but the first rule takes precedence and so the column affinity will be INTEGER.

In my case it's string which does not contain CHAR then the type is NUMERIC.