2017-04-21 40 views
2

在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包可供选择。教程已经有几年了,所以现在可能会更好。提前致谢。

回答

0

还有你从官方网站,如何在SQLite的使用外键有example启用。

+0

如果我执行查询来使用外键创建表而不使用SQLite.Net-PCL,我仍然可以使用SQLite.Net-PCL继续将ObservableCollections绑定到GridViews吗? – detailCode

+0

这对你想用数据库中存储的数据做什么没有影响。 – Szymson