如何在SQLite.net中存储对象列表? [英] How can you store lists of objects in SQLite.net?

查看:76
本文介绍了如何在SQLite.net中存储对象列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我有这两个对象

Let us assume I have these two objects

class Customer {
   [PrimaryKey]
   public string id;
   [??????]
   public List<int> addresses;
}

class Address {
     [PrimaryKey, AutoIncrement]
     public int id;
     public string street;
     public int number;
}

是否有一种使用SQLite.NET ORM来保存客户"对象的方法?因为我很难保存列表.

Is there a way to use the SQLite.NET ORM to save the Customers object? cause I'm having a really hard time saving lists.

如果没有这种方法,是否可以实现某种事件或可以重写的方法,以便在加载对象时触发代码?

If there is no such way, is there some sort of event I can implement or method I can override so that when an object gets loaded code will trigger?

我一直在考虑在地址列表上方添加[忽略]的方法,并且当事件触发时,我可以使用SQLite.net从另一个表中加载地址ID.

I was thinking something along the lines of adding [Ignore] above the list of addresses and when the event triggers I can use SQLite.net to load the ids of the addresses from another table

在此先感谢您提供的帮助

Thanks in advance for any help you can provide

推荐答案

此处

看看这个答案>

您可以使用 SQLite-Net扩展库

例如,在您的Model类中:

So for example in your Model class:

public class Customer 
{
   [PrimaryKey]
   public string id;
   [TextBlob("addressesBlobbed")]
   public List<int> addresses { get; set; }
   public string addressesBlobbed { get; set; } // serialized CoconutWaterBrands
}

有关文本过大"属性的文档:

from the documentation on Text blobbed properties:

被文本记录的属性在保存时被序列化为文本属性,在加载时被反序列化.这样可以将简单对象存储在同一表的单个列中.

Text-blobbed properties are serialized into a text property when saved and deserialized when loaded. This allows storing simple objects in the same table in a single column.

blob-blobed属性对对象进行序列化和反序列化的开销很小,并且有一些限制,但它是存储简单对象(如基本类型或简单关系的List或Dictionary)的最佳方法.文本标记属性需要一个声明的字符串属性,用于存储序列化对象.

Text-blobbed properties have a small overhead of serializing and deserializing the objects and some limitations, but are the best way to store simple objects like List or Dictionary of basic types or simple relationships. Text-blobbed properties require a declared string property where the serialized object is stored.

blob-blobed属性不能与其他对象建立关系,也不能与其父对象形成逆关系.

Text-blobbed properties cannot have relationships to other objects nor inverse relationship to its parent.

如果没有使用TextBlobOperations.SetTextSerializer方法指定其他序列化程序,则使用基于JSON的序列化程序.要使用JSON序列化程序,必须在项目中包含对Newtonsoft Json.Net库的引用,该引用也可以作为NuGet包提供.

A JSON-based serializer is used if no other serializer has been specified using TextBlobOperations.SetTextSerializer method. To use the JSON serializer, a reference to Newtonsoft Json.Net library must be included in the project, also available as a NuGet package.

希望这会有所帮助.

这篇关于如何在SQLite.net中存储对象列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆