vendredi 6 mai 2016

Why InsertAll in SQLITE crashes?

I am using InsertAll to save an ObservableCollection TickRecords into SQLITE database:

private static readonly ObservableCollection<TickRecord> TickRecords = new ObservableCollection<TickRecord>();

TickRecord is define like this:`

public class TickRecord : INotifyPropertyChanged
{
//
// Primary Key
//
private long _key;
[PrimaryKey]
public long Key
    {
    get
        {
        return _key;
        }
    set
        {
        _key = value;
        OnPropertyChanged( "Key" );
        }
    }


//
// Timestamp
//
private DateTime _tickStartDate;
public DateTime TickStartDate
    {
    get
        {
        return _tickStartDate;
        }
    set
        {
        _tickStartDate = value;
        OnPropertyChanged( "TickStartDate" );
        }
    }

private DateTime _tickEndDate;
public DateTime TickEndDate
    {
    get
        {
        return _tickEndDate;
        }
    set
        {
        _tickEndDate = value;
        OnPropertyChanged( "TickEndDate" );
        }
    }


//
// Record Duration in Seconds
//
private byte _durationInSeconds;
public byte DurationInSeconds
    {
    get
        {
        return _durationInSeconds;
        }
    set
        {
        _durationInSeconds = value;
        OnPropertyChanged( "DurationInSeconds" );
        }
    }

and saving into SQLITE is done like this:

    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.High, () => Database.Database.AddAllTickRecords( TickRecords ) );

and AddAllTickRecords is defined like this:

    public static void AddAllTickRecords( IEnumerable<TickRecord> list )
    {
    using ( var db = new SQLiteConnection( new SQLitePlatformWinRT(), DbPath ) )
        {
        db.InsertAll( list );
        }
    }

Any idea why I am getting an error here?

The error I getting from the debugger is:

    **An exception of type 'SQLite.Net.SQLiteException' occurred in SQLite.Net.dll but was not handled in user code
Additional information: Constraint**

and 

    **{SQLite.Net.SQLiteException: Constraint
   at SQLite.Net.PreparedSqlLiteInsertCommand.ExecuteNonQuery(Object[] source)
   at SQLite.Net.SQLiteConnection.Insert(Object obj, String extra, Type objType)
   at SQLite.Net.SQLiteConnection.Insert(Object obj)
   at SQLite.Net.SQLiteConnection.<>c__DisplayClass100_0.<InsertAll>b__0()
   at SQLite.Net.SQLiteConnection.RunInTransaction(Action action)
   at SQLite.Net.SQLiteConnection.InsertAll(IEnumerable objects, Boolean runInTransaction)
   at BTLE.Database.Database.AddAllTickRecords(IEnumerable`1 list)
   at BTLE.MainPage.<>c.<DecodeMessageResponse>b__123_0()}**

Aucun commentaire:

Enregistrer un commentaire