0

I am trying to set up a SQLite database using SQLite.Net and SQLiteNetExtensions. I cant seem to get the foreign keys to create in the database. I have dropped all tables and used the model classes to recreate them again. I have checked the pragma in SQLite manager and foreign keys are turned on. In my example, a plot can have many trees. Any suggestions on what i might be missing?

public class Tree
    {
        [PrimaryKey, AutoIncrement]
        public int TreeId { get; set; }
        [NotNull]
        public int TreeNumber { get; set; }
        [NotNull]
        public double Dbhob { get; set; }

        public double? Height { get; set; }
        [NotNull,Default(value: false)]
        public bool? HeightTree { get; set; }
        public string QualityCode { get; set; }
        [ForeignKey(typeof(Plot))]
        public int PlotId { get; set; }
        [ManyToOne]
        public Plot PlotInverse { get; set; }


    }



    public class Plot
    {
        [PrimaryKey, AutoIncrement]
        public int PlotId { get; set; }
        [NotNull, Unique]
        public System.Guid PlotGuid { get; set; }
        [NotNull, MaxLength(60)]
        public string Strata { get; set; }
        [NotNull, MaxLength(60)]
        public string PlotNumber { get; set; }

        public DateTime MeasureDate { get; set; }
        [MaxLength(70)]
        public String AssessorLead { get; set; }
        [MaxLength(60)]
        public String AssessorCrew { get; set; }
        [Default(value: 0)]
        public double PlotArea { get; set; }
        public double BasalArea { get; set; }

        [OneToMany("PlotId","PlotId")]
        public List<Tree> Trees { get; set; }
    }
Pankaj Kumar Katiyar
  • 1,474
  • 1
  • 20
  • 36
Ashley Goldstraw
  • 127
  • 1
  • 1
  • 4

1 Answers1

0

Foreign keys attributes are not created in the sqlite database as sqlite-net doesn't have support for Foreign Keys and SQLite-Net Extensions is built on top of it. They are used in runtime to save and load related objects.


As a side note, you should change this line:

[OneToMany("PlotId","PlotId")]
public List<Tree> Trees { get; set; }

To this:

[OneToMany("PlotId","PlotInverse")]
public List<Tree> Trees { get; set; }

or remove it all together and let auto-discovery do its magic:

[OneToMany]
public List<Tree> Trees { get; set; }
redent84
  • 18,901
  • 4
  • 62
  • 85