我在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