简体   繁体   English

使用 SQLite-Net Extensions,有没有办法让 List<string> ?</string>

[英]With SQLite-Net Extensions, is there a way to have List<string>?

In the documentation for SQLite-Net Extensions, it shows having an object (Stock) with property that has a List of another object (Valuation) with a OneToMany relationship.在 SQLite-Net Extensions 的文档中,它显示了一个 object(股票)的属性,该属性具有另一个 object(估值)的列表,具有 OneToMany 关系。 I am able to reproduce that fine.我能够重现那个罚款。

What I want to do is to be able to add a property to Stock that is a List of strings.我想要做的是能够向 Stock 添加一个属性,该属性是一个字符串列表。

But when I try to add a List<string> property, if I have no attributes on it, I get the error: 'Don't know about System.Collections.Generic.List`1[System.String]'但是当我尝试添加 List<string> 属性时,如果我没有属性,我会收到错误消息:'Don't know about System.Collections.Generic.List`1[System.String]'

If I add the [OneToMany] attribute to the property, I get the error: 'OneToMany relationships require Primary Key in the destination entity'如果我将 [OneToMany] 属性添加到属性,我会收到错误消息:“OneToMany 关系需要目标实体中的主键”

Is there any way that SQLite-Net Extensions can handle List<string> instead of List of non primative types? SQLite-Net Extensions 有什么方法可以处理 List<string> 而不是非原始类型的列表?

Any kind of relationship requires a destination table.任何一种关系都需要一个目标表。 In your case, a string is not stored as a table.在您的情况下, string不会存储为表。 You can either convert that value into a complex object that is stored in a separate table and use either a OneToMany or a ManyToMany or serialize the list as a string and save it in a different field.您可以将该值转换为存储在单独表中的复杂 object 并使用OneToManyManyToMany或将列表序列化为字符串并将其保存在不同的字段中。

For the latter case SQLite-Net Extensions includes a TextBlob attribute that does exactly that.对于后一种情况,SQLite-Net Extensions 包含一个TextBlob属性,它正是这样做的。 It serializes the property as a string before storing the objech to database and deserializes it after reading from database.它在将对象存储到数据库之前将属性序列化为字符串,并在从数据库读取后反序列化它。

public class Person
{
    public string Name { get; set; }

    [TextBlob("PhonesBlobbed")]
    public List<string> PhoneNumbers { get; set; }

    [TextBlob("AddressesBlobbed")]
    public List<Address> Addresses { get; set; }

    public string PhonesBlobbed { get; set; } // serialized phone numbers
    public string AddressesBlobbed { get; set; } // serialized addresses
}

More info in the manual.手册中的更多信息。

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

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