0

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

任何帮助将不胜感激。

+0

你的问题很混乱。您声明您尝试了两种不同的构造函数,但您也会说所有其他CRUD操作都可以工作。如果CRUD操作起作用,显然DB被创建,所以构造函数不是问题。你在Get操作中遇到错误吗?还是它只是返回null? – Jason 2015-03-24 23:54:03

+0

我不同意我的问题令人困惑。我分享了我采取的步骤,并指出我只有通过ID获取项目的问题,并且Visual Studio不会给我任何有意义的错误。无论如何感谢您的时间。 – 2015-03-25 00:21:41

回答

2

追逐阴影三天后,事实证明,这是一个非常简单的事情,让我绊倒。我绑保存对象的名单,像这样

[OneToMany(CascadeOperations = CascadeOperation.All)] 
public List<LineItem> LineItems { get; set; } 

我错过了重复的事实,SQLite.Net是一个轻量级的ORM的文档的一部分 - 这一点不能强调不够,所以你会必须删除您的全尺寸ORM帽子,如EF。因此之后,从SQLite的-Net的扩展文档它说

文本blobbed性质保存和加载时反序列化时 文本blobbed属性被序列化到一个文本属性读取。这允许将简单对象存储在同一个表中的单个列中。 文本blobbed属性具有序列化和反序列化对象和一些限制的小开销,但是存储简单对象(如List或Dictionary)的基本类型或简单关系的最佳方法。

我改变了我的窍门,就像这样,现在一切都按预期工作。现在处理异步和等待的细微差别

[TextBlob("LineItemBlobbed")] 
public List<LineItem> LineItems { get; set; } 

public string LineItemBlobbed { get; set; }