[英]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项目。
这是完全限定的类(命名空间和方法)。
刚刚添加:
SQLiteNetExtensions.Extensions.ReadOperations.GetAllWithChildren()
SQLiteNetExtensions.Extensions.WriteOperations.UpdateWithChildren()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.