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

标签 c# uwp sqlite-net sqlite-net-extensions

在 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

https://sqlite.org/foreignkeys.html

关于c# - 如何使用 SQLite.Net-PCL 制作 SQLite 外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43551933/

相关文章:

c# - Sqlite-net guid select 返回空 guid

c# - 如何使用 SQLite-Net Extensions 实现递归关系

c# - 无法删除 Windows 应用商店应用程序中的文件 - 访问被拒绝。 (HRESULT : 0x80070005 (E_ACCESSDENIED))

c# - wp7 应用程序中的 Web 客户端请求缓存问题

c# - Webservice IIS 和权限 401 错误

c# - 将集合 (HashSet) 隐式转换为 IEnumerable<T>

asynchronous - 如何在 UWP 中创建同步新闻源

C# 设置相对于 MainWindow 的 translatetransform X 和 Y 属性

c# - 如何在uwp中的禁用按钮上显示工具提示?

c# - 从 shape 派生的类给出了转换错误