2

我在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的错误库吗? http://forums.xamarin.com/discussion/20117/sqlite-net-extensions-and-sqliteconnection#latestXamarin Forms上的SQLite-Net扩展示例

这里是我的:两个Xamarin和SQLite-Net的扩展利用的NuGet,这是我觉得我有PCL版本...

我有这个贴在Xamarin论坛以及在此建议代码(除了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!"); 
      } 
     } 
    } 
} 

回答

3

SQLite-Net Extensions现在可用作MvvmCross的NuGet软件包和SQLite-Net的标准PCL版本。这应该解决这种类型的问题,其中库是用一个SQLite-Net版本编译的,但是用另一个版本执行,这可能会导致你描述的那类问题。

链接:

+0

我更新使用的NuGet版本。这是现在正确的答案。 – thedigitalsean

0

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

0

这里是完全限定类(命名空间和方法)。
只是说:

SQL Lite Extensions

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