c# - 如何使用 SQLite-Net 扩展建立 A -> B -> C 关系

标签 c# sqlite mvvmcross sqlite-net-extensions

我使用 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/

相关文章:

c# - Entity Framework 6 更新表并插入外键相关表

c# - 如何在MvvmCross中退订弱订阅

mvvmcross - 如何在 Windows Phone 上执行 CreateBindingSet()?

c# - Windows 窗体帮助按钮更改光标

c# - Principal Role App 引用的属性必须与 EntityType 的键完全相同

java - 如何使用 OrmLite 连接到受密码保护的 SQLite 数据库?

sql - 无法在 SQLite3 中创建触发器

objective-c - 打开sqlite数据库时内存泄漏

ios - Xamarin.ios 点击屏幕时关闭 UIAlertController ActionSheet

c# - 如果返回内容为 Transfer-Encoding :chunked?,如何从 HttpWebResponse 获取完整内容