外键不会使用SQLite.net在SQLite数据库中创建

时间:2016-04-15 05:58:30

标签: sqlite sqlite-net-extensions sqlite.net

我正在尝试使用SQLite.Net和SQLiteNetExtensions建立SQLite数据库。我似乎无法获得在数据库中创建的外键。我删除了所有表并使用模型类重新创建它们。我在SQLite管理器中检查了pragma,并打开了外键。在我的例子中,情节可以有很多树。关于我可能遗失的任何建议?

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; }
    }

1 个答案:

答案 0 :(得分:0)

外键属性不是在sqlite数据库中创建的,因为sqlite-net不支持外键,而SQLite-Net Extensions是建立在它之上的。它们在运行时用于保存和加载相关对象。

作为旁注,您应该更改此行:

Public Class myControlInputForm

        Private additionlHeight As Integer = 50
        Private sourceForm As Form

        Public Sub New(sourceForm As Form)
            InitializeComponent()

            Me.sourceForm = sourceForm

            Me.Height = Me.sourceForm.Height + additionlHeight
            Me.Width = Me.sourceForm.Width
            Me.BackColor = Me.sourceForm.BackColor

            Me.Refresh()
        End Sub 
End Class

对此:

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

或将它们全部删除,让自动发现发挥其魔力:

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