我正在我的 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/