我使用 MVVMCross 和 SQLite-Net 扩展包来完成以下任务:
public class StockGrouping
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Description { get; set; }
[OneToMany(CascadeOperations = CascadeOperation.All)] // One to many relationship with Valuation
public List<Stock> Stocks { get; set; }
}
public class Stock : IList<Valuation>
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[MaxLength(8)]
public string Symbol { get; set; }
[ForeignKey(typeof(StockGrouping))]
public int StockGroupingId { get; set; }
[OneToMany(CascadeOperations = CascadeOperation.All)] // One to many relationship with Valuation
public List<Valuation> Valuations { get; set; }
#region IList implementation
#endregion
}
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; }
}
基本上,我有一个整体结构,其中有一组股票类别,其中包含具有估值的股票。
我设置这个的想法是使用 iOS 分组 TableView 将股票分组到特定类别下。
使用 SQLite-Net 扩展可以进行这种设置吗?
此时,当我像这样创建 SQLite 表时遇到异常:
private void CreateDataBase()
{
sqLiteConnection.CreateTable<Stock>();
sqLiteConnection.CreateTable<StockGrouping>();
sqLiteConnection.CreateTable<Valuation>();
}
它在 sqLiteConnection.CreateTable() 行上失败,表示它不理解 StockGrouping。
有什么想法吗?这可能吗?
最佳答案
SQLite-Net 似乎不支持从 IList
继承的对象。更改 Stock
定义:
public class Stock : IList<Valuation>
至
public class Stock
让它在我的测试中工作。
您还可以继承 IEnumerable
而不是 IList
并且它可以工作:
public class Stock : IEnumerable<Valuation>
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[MaxLength(8)]
public string Symbol { get; set; }
[ForeignKey(typeof(StockGrouping))]
public int StockGroupingId { get; set; }
[OneToMany(CascadeOperations = CascadeOperation.All)] // One to many relationship with Valuation
public List<Valuation> Valuations { get; set; }
public IEnumerator<Valuation> GetEnumerator() {
return Valuations.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
return Valuations.GetEnumerator();
}
}
关于c# - 如何使用 SQLite-Net 扩展建立 A -> B -> C 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26749517/