Cru*_*ces 8 c# xamarin sqlite.net
我们假设我有这两个对象
class Customer {
[PrimaryKey]
public string id;
[??????]
public List<int> addresses;
}
Run Code Online (Sandbox Code Playgroud)
和
class Address {
[PrimaryKey, AutoIncrement]
public int id;
public string street;
public int number;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法使用SQLite.NET ORM来保存Customers对象?因为我很难保存列表.
如果没有这种方式,是否有某种我可以实现的事件或我可以覆盖的方法,以便当一个对象被加载时代码会触发?
我正在考虑在地址列表上方添加[忽略]的方法,当事件触发时我可以使用SQLite.net从另一个表加载地址的ID
提前感谢您提供的任何帮助
Iai*_*ith 10
在这里看看这个答案
您可以使用SQLite-Net Extensions库中的 Text blobbed属性来执行此操作
例如,在您的Model类中:
public class Customer
{
[PrimaryKey]
public string id;
[TextBlob("addressesBlobbed")]
public List<int> addresses { get; set; }
public string addressesBlobbed { get; set; } // serialized CoconutWaterBrands
}
Run Code Online (Sandbox Code Playgroud)
从文本blobbed属性的文档:
文本 - blobbed属性在加载时被序列化为文本属性,并在加载时反序列化.这允许在单个列中将简单对象存储在同一个表中.
文本blobbed属性具有序列化和反序列化对象的一小部分开销和一些限制,但是存储基本类型的List或Dictionary等简单对象或简单关系的最佳方法.Text-blobbed属性需要一个声明的字符串属性,其中存储了序列化对象.
文本blobbed属性不能与其他对象建立关系,也不能与其父对象建立反向关系.
如果未使用TextBlobOperations.SetTextSerializer方法指定其他序列化程序,则使用基于JSON的序列化程序.要使用JSON序列化程序,必须在项目中包含对Newtonsoft Json.Net库的引用,也可以作为NuGet包使用.
希望这可以帮助.