C#SQLite扩展InsertWithChildren不起作用的原因是什么?

时间:2015-12-30 12:01:42

标签: c# sqlite

我的SQLite-Database存在问题。我使用SQLite.Net和SQLiteNetExtensions PCL NuGets并遵循此示例:Link

我有一个连接,它插入了一个股票而不是估价清单。

我的复制粘贴代码:

var valuation1 = new Valuation
        {
            Price = 15,
            Time = DateTime.Now,
        };

        var valuation2= new Valuation
        {
            Price = 22,
            Time = DateTime.Now,
        };

        var euro = new Stock
        {
            Symbol = "€",
            Valuations = new List<Valuation> { valuation1, valuation2 }
        };

        connection.CreateTable<Stock>();
        connection.CreateTable<Valuation>();

        connection.InsertWithChildren(euro);

        var stockList = c.GetAllWithChildren<Stock>();

在Stocklist中是股票,但在股票中没有估价:(

usings:

using System; using System.Collections.Generic;
using System.Linq;
using SQLiteNetExtensions.Extensions;
using TestSQLite.Models;

型号:

using SQLite.Net.Attributes;
using SQLiteNetExtensions.Attributes;
using System;
using System.Collections.Generic;
public class Stock
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    [MaxLength(8)]
    public string Symbol { get; set; }

    [OneToMany]      // One to many relationship with Valuation
    public List<Valuation> Valuations { get; set; }
}
using SQLite.Net.Attributes;
using SQLiteNetExtensions.Attributes;
using System;
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; }
}

我可以采取哪些建议或建议来使其发挥作用?

编辑: 我只是尝试了一些东西,如果我像我一样对自己感到不安:

c.Insert(euro);
        var e = c.Find<Stock>(s => s.Symbol == "€");

        var valuation1 = new Valuation
        {
            Price = 15,
            Time = DateTime.Now,
            StockId = e.StockId,
            Stock = e
        };

        var valuation2 = new Valuation
        {
            Price = 22,
            Time = DateTime.Now,
            StockId = e.StockId,
            Stock = e
        };

        c.Insert(valuation1);
        c.Insert(valuation2);

var stockList = c.GetAllWithChildren<Stock>();

我得到了正确的股票&#34;欧元&#34;和它的价值!我在InsertWithChildren的错误是什么?或者我的模型有什么问题吗?

0 个答案:

没有答案