【问题标题】:SQLite-Net Extensions Example on Xamarin FormsXamarin 表单上的 SQLite-Net 扩展示例
【发布时间】:2014-08-31 03:57:38
【问题描述】:

我对将 SQlite-Net Extensions (https://bitbucket.org/twincoders/sqlite-net-extensions) 与 Xamarin Forms 一起使用很感兴趣。我正在使用 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 扩展都建议使用来自 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; }
    }
}

这是我的数据库助手。现在我只是在构造函数中运行一个测试。我得到的错误是找不到方法 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 xamarin xamarin.forms sqlite-net sqlite-net-extensions


    【解决方案1】:

    SQLite-Net 扩展现在可用作 MvvmCross 和 SQLite-Net 的标准 PCL 风格的 NuGet 包。这应该可以解决此类问题,即库是使用一个 SQLite-Net 版本编译但使用另一个版本执行的,这可能会导致您所描述的问题。

    链接:

    【讨论】:

    • 我更新为使用 Nuget 版本。这是正确的答案。
    【解决方案2】:

    来自 Xamarin 支持的 Jon Goldberger 为我指明了正确的方向。对于那些将来发现这篇文章的人来说,简而言之,解决方案是除非您使用 MvvmCross,否则您需要从源代码构建 SQLite-Net 扩展,不要使用预编译的 dll。从他们的 git 服务器中提取后,您需要添加 SQLiteNetExtensions-PCL.csproj,使用 Project->Restore Packages 恢复它的包,然后从您的基础项目中引用 SQLiteNetExtensions-PCL 项目。

    【讨论】:

      【解决方案3】:

      这是完全限定的类(命名空间和方法)。
      刚刚补充:

      SQL Lite Extensions

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

      【讨论】:

        猜你喜欢
        • 2015-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多