在 UWP 中,我享受使用 SQLite.Net-PCL 的好处,创建要在应用程序中用作 ObservableCollections 的类以绑定(bind)到 GridView。在包含 SQLiteNetExtensions 以使用外键构建数据库之后,我注意到在 SQLite Maestro 中查看数据库时并没有真正创建外键。而是创建索引。 如果不真正创建外键,使用 SQLiteNetExtensions 有什么好处?
当使用 LAMDA 表达式或 LINQ 查询时,可能不需要外键(稍后在创建数据库后的应用程序中)。 如果我在不使用 SQLite.Net-PCL 的情况下执行查询以使用外键创建表,我是否仍可以使用 SQLite.Net-PCL 继续将 ObservableCollections 绑定(bind)到 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; }
}
SQLite 对我来说是个新手,可供选择的 SQLite Nuget 包非常多。教程已有几年历史,所以现在可能会有更好的东西。提前致谢。
最佳答案
即使您将 Entity Framework 核心与 UWP 应用一起用于数据访问,外键也不可用。 SQLite 默认不启用外键
https://learn.microsoft.com/en-us/ef/core/providers/sqlite/limitations
关于c# - 如何使用 SQLite.Net-PCL 制作 SQLite 外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43551933/