在UWP中,我享受使用SQLite.Net-PCL的好处,创建用于应用程序的类作为ObservableCollections绑定到GridView。在包含SQLiteNetExtensions以构建带有外键的数据库之后,我注意到在SQLite Maestro中查看数据库时,并未真正创建外键。索引被创建。 如果不真的创建外键,使用SQLiteNetExtensions有什么好处?如何使SQLite外键与SQLite.Net-PCL
查询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; }
}
SQLite是新的给我,有这么多的SQLite的NuGet包可供选择。教程已经有几年了,所以现在可能会更好。提前致谢。
但是你可以在SQLite v3.0 +中启用外键?例如:conn.Execute(“PRAGMA foreign_keys = ON”); – detailCode
@detailCode应该是实际的答案。 SQLite不支持外键,但默认情况下禁用... – Tomas