2

SQlite-Net 拡張機能の使用に興味があります ( https://bitbucket.org/twincoders/sqlite-net-extensions) Xamarin フォームで。Visual Studio 2013、NuGet の SQlite.Net PCL 2.3.0、および空白の Hello World Xamarin Forms PLC プロジェクトを使用しています。最初のステップとして、Sqlite.Net 拡張サイトの例を機能させようとしています。Xamarin Web サイトで推奨されている方法に従って、DependencyService を使用して SQLIteConnection を取得しています。ただし、SQLiteConnection で UpdateWithChildren メソッドも GetWithChildren メソッドも見つからないため、コードはコンパイルされません。私の SQLiteConnection オブジェクトが例と同じものをすべて持っているわけではないことは明らかです。SQLite.Net PCL に間違ったライブラリを使用していますか? Xamarin と SQLite-Net 拡張機能の両方が、NuGet の PCL バージョンを使用することを提案しました。これは、私が持っていると思うものです...

これを Xamarin フォーラムにも投稿しました: http://forums.xamarin.com/discussion/20117/sqlite-net-extensions-and-sqliteconnection#latest

これが私のコードです(ISQLiteクラスを除く)。データ モデル:

using SQLiteNetExtensions.Attributes;
using SQLite.Net.Attributes;


namespace Sample.Models
{
    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; }
    }
}

そして、これが私の DatabaseHelper です。現在、コンストラクターでテストを実行しているだけです。私が得るエラーは、メソッド UpdateWithChildren と GetWithChildren が見つからないということです。

using Sample.Models;
using SQLite.Net;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace Sample.Orm
{
    class DatabaseHelper
    {
        private SQLiteConnection db;

        public DatabaseHelper()
        {
            db = DependencyService.Get<ISQLite>().GetConnection();
            //Create the tables

            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!");
            }
        }
    }
}
4

3 に答える 3