Fix*_*xus 7 c# sqlite microsoft-metro windows-runtime windows-store-apps
我正在Windows Store应用程序(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>();
}
}
Run Code Online (Sandbox Code Playgroud)
我明白了
- $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
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么 ?
Bob*_*son 10
只是为了跟进我对更多研究的评论 - SQLite-net不支持任何无法直接映射到数据库的东西.看这里是为什么:
ORM能够获取.NET类定义并将其转换为SQL表定义.(大多数ORM都朝另一个方向发展.)它通过检查类的所有公共属性来完成此操作,并由可用于指定列详细信息的属性辅助.
您可以考虑使用不同的ORM来实际访问您的数据(我使用Vici Coolstorage),如果您正在尝试这样做,或者只是List<Chapters>
从您的类中删除并向该类添加一个BookID
字段Chapters
.这就是数据库如何代表它.
为了使用它,您可以将其中一个添加到您的类:
List<Chapters> Chapters {
get {
return db.Query<Chapters> ("select * from Chapters where BookId = ?", this.Id);
}
}
Run Code Online (Sandbox Code Playgroud)
要么
List<Chapters> Chapters {
get {
return db.Query<Chapters>.Where(b => b.BookId == this.Id);
}
}
Run Code Online (Sandbox Code Playgroud)
这至少可以让你轻松地拉出列表,虽然它会很慢,因为它每次访问时都会访问数据库.
看一下SQLite-Net Extensions.它通过使用反射在SQLite-Net之上提供复杂的关系.
从站点中提取的示例:
public class Stock
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[MaxLength(8)]
public string Symbol { get; set; }
[OneToMany] // One to many relationship with Valuation
public List<Valuation> Valuations { get; set; }
}
public class Valuation
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[ForeignKey(typeof(Stock))] // Specify the foreign key
public int StockId { get; set; }
public DateTime Time { get; set; }
public decimal Price { get; set; }
[ManyToOne] // Many to one relationship with Stock
public Stock Stock { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6383 次 |
最近记录: |