我很努力地使用SQLite.Net Async PCL的異步API通過ID獲取項目。這裏是我的模型類無法從SQLite.Net異步獲取項目PCL
public class Invoice : IEntityBase
{
public Invoice()
{
LineItems = new List<LineItem>();
DateCreated = DateTime.Now;
}
[PrimaryKey, AutoIncrement, Column("_id")]
public int Id { get; set; }
public DateTime DateCreated { get; set; }
public int Term { get; set; }
public bool Paid { get; set; }
public decimal Total { get; set; }
public string Notes { get; set; }
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<LineItem> LineItems { get; set; }
}
而且,這裏有一個一對多的關係
[PrimaryKey, AutoIncrement, Column("_id")]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
public int Qty { get; set; }
[ForeignKey(typeof(Invoice))]
public int InvoiceId { get; set; }
[ManyToOne]
public Invoice Invoice { get; set; }
這裏的構造函數了LineItem:
public SQLiteAsyncConnection DbConnection;
public InvoiceDatabase(ISQLitePlatform platform, string databasePath)
{
if (DbConnection == null)
{
var connectionAsync = new Func<SQLiteConnectionWithLock>(() =>
new SQLiteConnectionWithLock
(
platform,
new SQLiteConnectionString(databasePath, false)
)
);
DbConnection = new SQLiteAsyncConnection(connectionAsync);
DbConnection.CreateTableAsync<Invoice>();
DbConnection.CreateTableAsync<LineItem>();
}
}
其他CRUD方法(插入, GetALL)正在工作,除了通過ID獲取發票之外,Visual Studio和Xamarin Studio都沒有給我任何有用的堆棧跟蹤。
這裏是GET方法
private readonly InvoiceDatabase _database;
public InvoiceRepository(ISQLitePlatform platform, string databasePath)
{
if (_database == null)
{
_database = new InvoiceDatabase(platform, databasePath);
}
}
public async Task<Invoice> GetInvoice(int id)
{
var result = await _database.DbConnection.Table<Invoice>()
.Where(t => t.Id == id)
.FirstOrDefaultAsync();
return result;
}
我傳遞在Android實施的SQLite的,就像我說的數據庫中創建,但我無法獲得發票對象回來了,我甚至嘗試
public Task<Invoice> GetInvoiceWithChildren(int id)
{
return _database.DbConnection.GetWithChildrenAsync<Invoice>(id);
}
任何幫助將不勝感激。
你的問題很混亂。您聲明您嘗試了兩種不同的構造函數,但您也會說所有其他CRUD操作都可以工作。如果CRUD操作起作用,顯然DB被創建,所以構造函數不是問題。你在Get操作中遇到錯誤嗎?還是它只是返回null? – Jason 2015-03-24 23:54:03
我不同意我的問題令人困惑。我分享了我採取的步驟,並指出我只有通過ID獲取項目的問題,並且Visual Studio不會給我任何有意義的錯誤。無論如何感謝您的時間。 – 2015-03-25 00:21:41