jeudi 10 septembre 2015

How to retrieve List from SQLite Extensions Windows Store

I've installed SQLiteNetExtensions from NuGet; this is the only extension I have installed. Following the example here, I have the code working - database saves all the data into the database, with the exception of Lists, it seems. I'd like to retrieve the List from the storedValuation variable, however it is appearing as null in debug mode. Is there any way to retrieve this List?

SQLiteConnection db = new SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), App.dbPath);
public static string dbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "TestSQLDB.sqlite");

Function:

public void DatabaseFunction()
    {
        db.CreateTable<Stock>();
        db.CreateTable<Valuation>();

        var euro = new Stock()
        {
            Symbol = "€"
        };
        db.Insert(euro);   // Insert the object in the database

        var valuation = new Valuation()
        {
            Price = 15,
            Time = DateTime.Now,
        };
        db.Insert(valuation);   // Insert the object in the database

        // Objects created, let's stablish the relationship
            euro.Valuations = new List<Valuation> { valuation };

        db.UpdateWithChildren(euro);   // Update the changes into the database
        if (valuation.Stock == euro)
        {
            Debug.WriteLine("Inverse relationship already set, yay!");
        }

        // Get the object and the relationships
        var storedValuation = db.GetWithChildren<Valuation>(valuation.Id);
        if (euro.Symbol.Equals(storedValuation.Stock.Symbol))
        {
            Debug.WriteLine("Object and relationships loaded correctly!");
        }
    }

Classes:

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; }
}

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; }
}

enter image description here

Aucun commentaire:

Enregistrer un commentaire