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

det*_*ode 9 c# sqlite-net sqlite-net-extensions uwp

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

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

示例数据库:

[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; }
}
Run Code Online (Sandbox Code Playgroud)

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

Ken*_*ker 2

即使您将实体框架核心与 UWP 应用程序一起用于数据访问,外键也不可用。默认情况下,SQLite 中未启用外键

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

https://sqlite.org/foreignkeys.html

  • 但是你可以在 SQLite v3.0+ 中启用外键,对吧?示例: conn.Execute("PRAGMAforeign_keys = ON"); (2认同)