簡體   English   中英

Xamarin表單上的SQLite-Net擴展示例

[英]SQLite-Net Extensions Example on Xamarin Forms

我對Xamarin Forms的SQlite-Net擴展( https://bitbucket.org/twincoders/sqlite-net-extensions )感興趣。 我正在使用Visual Studio 2013,NuGet的SQlite.Net PCL 2.3.0和空白的Hello World Xamarin Forms PLC項目。 第一步,我試圖從Sqlite.Net擴展站點上獲取示例。 根據Xamarin網站上建議的方法,我正在使用DependencyService獲取SQLIteConnection。 但是,我的代碼甚至無法編譯,因為它無法在SQLiteConnection上找到UpdateWithChildren或GetWithChildren方法。 顯而易見,我的SQLiteConnection對象沒有與示例完全相同的內容。 我是否為SQLite.Net PCL使用了錯誤的庫? Xamarin和SQLite-Net Extensions都建議使用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!");
            }
        }
    }
}

SQLite-Net擴展現在作為NuGet軟件包提供,用於MvvmCross和SQLite-Net的標准PCL版本。 如果庫是使用一個SQLite-Net版本編譯但使用另一個SQLite-Net版本執行的,則應該解決這種問題,這可能會導致您描述的問題。

鏈接:

Xamarin支持人員Jon Goldberger向我指出了正確的方向。 對於那些將來會找到這篇文章的人,簡而言之,解決方案是,除非您使用MvvmCross,否則您需要從源代碼構建SQLite-Net擴展,不要使用預編譯的dll。 從他們的git服務器中拉出后,您需要添加SQLiteNetExtensions-PCL.csproj,使用Project-> Restore Packages還原它的包,然后從基礎項目中引用SQLiteNetExtensions-PCL項目。

這是完全限定的類(命名空間和方法)。
剛剛添加:

SQL Lite擴展

SQLiteNetExtensions.Extensions.ReadOperations.GetAllWithChildren()
SQLiteNetExtensions.Extensions.WriteOperations.UpdateWithChildren()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM