如何使用 SQLite.Net-PCL 制作 SQLite 外键

How to make SQLite foreign keys with SQLite.Net-PCL

在 UWP 中,我享受使用 SQLite.Net-PCL 的好处,创建 类 在应用程序中用作 ObservableCollections 以绑定到 GridView。在包含 SQLiteNetExtensions 以使用外键构建数据库之后,我注意到在 SQLite Maestro 中查看数据库时并没有真正创建外键。而是创建索引。 如果不真正创建外键,使用 SQLiteNetExtensions 有什么好处?

当使用 LAMDA 表达式或 LINQ 查询时,可能不需要外键(稍后在创建数据库后的应用程序中)。 如果我在不使用 SQLite.Net-PCL 的情况下使用外键创建表,我是否仍然可以使用 SQLite.Net-PCL 继续将 ObservableCollections 绑定到网格视图?

示例数据库:

[Table("Book")]
public class Book
{
    [PrimaryKey, AutoIncrement, Column("ID")]
    public int ID { get; set; }
    [Column("Name")]
    public string Name { get; set; }

    [ManyToMany]
    public List<Checkout> Checkout { get; set; }
}

[Table("School")]
public class School
{
    [PrimaryKey, AutoIncrement, Column("ID")]
    public int ID { get; set; }
    [Column("Name")]
    public string Name { get; set; }

    [OneToMany]
    public List<Student> Student { get; set; }
    [ManyToMany]
    public List<Checkout> Checkout { get; set; }
}

[Table("Student")]
public class Student
{
    [PrimaryKey, AutoIncrement, Column("ID")]
    public int ID { get; set; }
    [Column("SchoolID"), ForeignKey(typeof(School))]
    public int SchoolID { get; set; }
    [Column("Name")]
    public string Name { get; set; }

    [ManyToOne]
    public School School { get; set; }
}

[Table("Checkout")]
public class Checkout
{
    [PrimaryKey, AutoIncrement, Column("ID")]
    public int ID { get; set; }
    [Column("SchoolID"), ForeignKey(typeof(School))]
    public int SchoolID { get; set; }
    [Column("BookID"), ForeignKey(typeof(Book))]
    public int BookID { get; set; }
}

SQLite 对我来说是个新手,可供选择的SQLite Nuget 包非常多。教程已有几年历史,所以现在可能会有更好的东西。提前致谢。

即使您将 entity framework 核心与 UWP 应用一起用于数据访问,外键也不可用。 SQLite

默认不启用外键

https://docs.microsoft.com/en-us/ef/core/providers/sqlite/limitations

https://sqlite.org/foreignkeys.html

官方网站上有 example 如何在 SQLite 中使用外键。