简体   繁体   English

Xamarin表单上的SQLite-Net扩展示例

[英]SQLite-Net Extensions Example on Xamarin Forms

I am intersted in using SQlite-Net Extensions ( https://bitbucket.org/twincoders/sqlite-net-extensions ) with Xamarin Forms. 我对Xamarin Forms的SQlite-Net扩展( https://bitbucket.org/twincoders/sqlite-net-extensions )感兴趣。 I am using Visual Studio 2013, SQlite.Net PCL 2.3.0 from NuGet, and a blank Hello World Xamarin Forms PLC project. 我正在使用Visual Studio 2013,NuGet的SQlite.Net PCL 2.3.0和空白的Hello World Xamarin Forms PLC项目。 As a first step I am trying to just get the example from the Sqlite.Net extensions site working. 第一步,我试图从Sqlite.Net扩展站点上获取示例。 As per the suggested method on the Xamarin website I am using DependencyService to get the SQLIteConnection. 根据Xamarin网站上建议的方法,我正在使用DependencyService获取SQLIteConnection。 However, my code will not even compile since it cannot find UpdateWithChildren nor GetWithChildren methods on the SQLiteConnection. 但是,我的代码甚至无法编译,因为它无法在SQLiteConnection上找到UpdateWithChildren或GetWithChildren方法。 It seems obvious that my SQLiteConnection object does not have all the same things as the example. 显而易见,我的SQLiteConnection对象没有与示例完全相同的内容。 Am I using the wrong library for SQLite.Net PCL? 我是否为SQLite.Net PCL使用了错误的库? Both Xamarin and SQLite-Net Extensions suggested using the PCL version from NuGet, which is what I think I have... Xamarin和SQLite-Net Extensions都建议使用NuGet的PCL版本,这是我认为的...

I have this posted on the Xamarin Forums as well here: http://forums.xamarin.com/discussion/20117/sqlite-net-extensions-and-sqliteconnection#latest 我在这里也将其发布在Xamarin论坛上: http : //forums.xamarin.com/discussion/20117/sqlite-net-extensions-and-sqliteconnection#latest

Here is my code (except for the ISQLite class). 这是我的代码(ISQLite类除外)。 Data Models: 数据模型:

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

And here is my DatabaseHelper. 这是我的DatabaseHelper。 Right now I'm just running a test right in the constructor. 现在,我只是在构造函数中运行测试。 The error I get is that the methods UpdateWithChildren and GetWithChildren cannot be found. 我得到的错误是找不到方法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 Extensions is now available as NuGet packages for MvvmCross and standard PCL flavors of SQLite-Net. SQLite-Net扩展现在作为NuGet软件包提供,用于MvvmCross和SQLite-Net的标准PCL版本。 This should fix this kind of issues where the library was compiled with one SQLite-Net version but executed with another, that could cause the kind of issues that you are describing. 如果库是使用一个SQLite-Net版本编译但使用另一个SQLite-Net版本执行的,则应该解决这种问题,这可能会导致您描述的问题。

The links: 链接:

Jon Goldberger from Xamarin support pointed me in the right direction. Xamarin支持人员Jon Goldberger向我指出了正确的方向。 For those finding this post in the future, the solution in short, is that unless you are using MvvmCross you need to build SQLite-Net Extensions from source, do not use the precompiled dll. 对于那些将来会找到这篇文章的人,简而言之,解决方案是,除非您使用MvvmCross,否则您需要从源代码构建SQLite-Net扩展,不要使用预编译的dll。 Once you pull from their git server, you need to add the SQLiteNetExtensions-PCL.csproj, restore it's packages with Project->Restore Packages and then reference the SQLiteNetExtensions-PCL project from your base project. 从他们的git服务器中拉出后,您需要添加SQLiteNetExtensions-PCL.csproj,使用Project-> Restore Packages还原它的包,然后从基础项目中引用SQLiteNetExtensions-PCL项目。

Here is the fully qualify class (namespace and method). 这是完全限定的类(命名空间和方法)。
Just added: 刚刚添加:

SQL Lite Extensions SQL Lite扩展

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM