我很努力地使用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