error creating a table with sqlite-net-pcl

Hi,

This code works well:

    public class SettingsDatabase
    {
        readonly SQLiteAsyncConnection database;

        private List<ConstantCorrection> constantCorrections { get; set; }

        public SettingsDatabase(string dbPath)
        {
            database = new SQLiteAsyncConnection(dbPath);
            database.CreateTableAsync<ConstantCorrection>().Wait(); //            GREAT, WORKS WELL!

But that one fails:

    public class PositionsSightsDatabase
    {
        readonly SQLiteAsyncConnection database;

        public PositionsSightsDatabase(string dbPath)
        {
            database = new SQLiteAsyncConnection(dbPath);
            database.CreateTableAsync<Position>().Wait();     //                   <---- FAILS
            database.CreateTableAsync<Sight>().Wait();
        }

This is the Position class:

using System;
using System.Runtime.CompilerServices;
using System.ComponentModel;
using SQLite;

using AlmicantaratSharedProject.MathTools;

namespace AlmicantaratXF.Model
{
    public enum PositionType : int { Satellite = 0, Celestial = 1, DeadReckoning = 2, Inertial = 3 }

    public class Position : INotifyPropertyChanged, IComparable<Position>
    {
        public event PropertyChangedEventHandler PropertyChanged;

        [PrimaryKey, AutoIncrement]
        public int ID { get; set; }
        public DateTime DateTimeGroup { get; set; }
        public Vessel Vessel { get; set; }
        public PositionType PositionType { get; set; }
        public LatitudeLongitude LatLon { get; set; }
        public Position()
        {
            //DateTimeGroup = DateTime.Now;
            Vessel = new Vessel();
            PositionType = PositionType.Satellite;
            LatLon = new LatitudeLongitude();
        }
        public string StrLat
        {
            get => LatLon.Lat.ToString();
        }
        public string StrLon
        {
            get => LatLon.Lon.ToString();
        }
        protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public int CompareTo(Position compared)
        {
            return this.DateTimeGroup.CompareTo(compared.DateTimeGroup); //TODO
        }
    }
}

Best Answer

Answers

  • jddjdd USMember ✭✭✭

    To find the issue, I added Interfaces, methods and properties to the class until it bugs.
    This is the problematic property:

    using System;
    using SQLite;
    using System.Runtime.CompilerServices;
    using System.ComponentModel;
    
    namespace AlmicantaratXF.Model
    {
        public class DebugPosition : INotifyPropertyChanged, IComparable<DebugPosition>
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            [PrimaryKey, AutoIncrement]
            public int ID { get; set; }
            public DateTime DateTimeGroup { get; set; }
            public Vessel Vessel { get; set; } //               <-- That propertie makes the CreateTableAsync<DebugPosition> bug
            public string StrLat { get; set; }
            public string StrLon { get; set; }
            public DebugPosition()
            {
            }
            protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
            public int CompareTo(DebugPosition compared)
            {
                return this.ID.CompareTo(compared.ID); //TODO
            }
        }
    }
    
    

    This is the Vessel class:

        public class Vessel
        {
            public string IMO { get; set; }
            public string Name { get; set; }
            public string CallSign { get; set; }
            public string Flag { get; set; }
            public Vessel()
            {
                IMO = "No IMO";
                Name = "Vessel Name";
                CallSign = "Call Sign";
                Flag = "ISO 3166 two-letter country code";
            }
        }
    
    

    Very simple indeed.
    Why can't I add a own class property to a database item?

  • jddjdd USMember ✭✭✭
    Accepted Answer

    Solution here:
    https://github.com/praeclarum/sqlite-net/issues/824
    Only some classes can be stored. Not own classes.

Sign In or Register to comment.