我在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!");
}
}
}
}
我更新使用的NuGet版本。这是现在正确的答案。 – thedigitalsean