c# - 如何创建集合 (1 :n) relation

标签 c# sqlite microsoft-metro windows-runtime windows-store-apps

我正在我的 Windows 应用商店应用程序 (WinRT) 中实现 SQLite 数据库。 我想要两个表之间的关系 (1:n) 书 (1) - 第 (n) 章

class Book
{
    [SQLite.AutoIncrement, SQLite.PrimaryKey]
    public int Id { get; set; }
    public String Title { get; set; }
    public String Description { get; set; }
    public String Author { get; set; }
    public List<Chapter> Chapters { get; set; }

    public Book() 
    {
        this.Chapeters = new List<Chapter>();
    }
}

我明白了

-       $exception  {"Don't know about     System.Collections.Generic.List`1[Audioteka.Models.Chapter]"}    System.Exception {System.NotSupportedException}

+       [System.NotSupportedException]  {"Don't know about System.Collections.Generic.List`1[Audioteka.Models.Chapter]"}    System.NotSupportedException

+       Data    {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
    HelpLink    null    string
    HResult -2146233067 int
+       InnerException  null    System.Exception
    Message "Don't know about System.Collections.Generic.List`1[Audioteka.Models.Chapter]"  string

我做错了什么?

最佳答案

只是为了通过更多研究来跟进我的评论 - SQLite-net 不支持任何不能直接映射到数据库的东西。参见 here为什么:

The ORM is able to take a .NET class definition and convert it to a SQL table definition. (Most ORMs go in the other direction.) It does this by examining all public properties of your classes and is assisted by attributes that you can use to specify column details.

您可以考虑使用不同的 ORM 来实际访问您的数据(我使用 Vici Coolstorage ),如果这是您想要做的,或者只是删除 List<Chapters>从你的类(class)并添加一个BookID字段到 Chapters类(class)。这就是数据库表示它的方式。

为了使用它,您可以将其中之一添加到您的类中:

List<Chapters> Chapters { 
  get { 
     return db.Query<Chapters> ("select * from Chapters where BookId = ?", this.Id); 
  } 
}

List<Chapters> Chapters { 
  get { 
     return db.Query<Chapters>.Where(b => b.BookId == this.Id); 
  } 
}

这至少可以让您轻松地提取列表,尽管它会很慢,因为它会在您每次访问时访问数据库。

关于c# - 如何创建集合 (1 :n) relation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13145578/

相关文章:

c# - 将规则条件转换为规则列表

c# - 从代码访问 div 元素

node.js - NodeJS - sqlite3,数据同步

c# - 处理 Windows 8 弹出窗口的最佳方法是 light dismissed?

javascript - Windows 8 html/js : Alternative to <img>/removing the 'dragging' functionality?

c# - 我应该在应用程序退出之前运行 Dispose 吗?

c# - 我如何以指数方式表示 c# 图表轴值?

sql - PRAGMA table_info(table-name) 在 SQLite 数据库中如何工作?

python - 在 Django 中迁移到 mysql

user-interface - Metro UI 是否受版权保护或者可以实现吗?