简体   繁体   English

如何创建集合(1:n)关系

[英]how to create collection (1:n) relation

I'm implementing SQLite database in my Windows Store application (WinRT). 我正在Windows Store应用程序(WinRT)中实现SQLite数据库。 I want to relation between two tables (1:n) Book (1) - Chapter (n) 我想要两个表之间的关系(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>();
    }
}

I get 我明白了

-       $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

What am I doing wrong ? 我究竟做错了什么 ?

Just to follow up on my comment with a bit more research - SQLite-net doesn't support anything which can't be directly mapped to the database. 仅仅通过更多研究来跟进我的评论 - SQLite-net不支持任何无法直接映射到数据库的内容。 See here for why: 这里是为什么:

The ORM is able to take a .NET class definition and convert it to a SQL table definition. ORM能够获取.NET类定义并将其转换为SQL表定义。 (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都朝另一个方向发展。)它通过检查类的所有公共属性来完成此操作,并由可用于指定列详细信息的属性辅助。

You can look into using a different ORM to actually access your data (I use Vici Coolstorage ), if that's what you're trying to do, or simply remove the List<Chapters> from your class and add a BookID field to the Chapters class. 您可以考虑使用不同的ORM来实际访问您的数据(我使用Vici Coolstorage ),如果您正在尝试这样做,或者只是从您的类中删除List<Chapters>并将BookID字段添加到Chapters类。 That's how the database would represent it. 这就是数据库如何代表它。

For purposes of working with it, you could add one of these to your class: 为了使用它,您可以将其中一个添加到您的类:

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

or 要么

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

That would at least let you pull the list easily, although it would be slow because it hits the database every time you access it. 这至少可以让你轻松地拉出列表,虽然它会很慢,因为它每次访问时都会访问数据库。

Take a look at SQLite-Net Extensions . 看一下SQLite-Net Extensions It provides complex relationships on top of SQLite-Net by using reflection. 它通过使用反射在SQLite-Net之上提供复杂的关系。

Example extracted from the site: 从站点中提取的示例:

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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM