2016-09-16 122 views
0

我想将我的数据保存在SQLite数据库中,所以我安装了SQLite.Net-PLC nuget,并且我在android和iOS中创建了一个连接,然后我安装了SQLite.Net.Async- PLC的NuGet并创建了一个仓库,在那里我定义功能:插入,更新和删除这样的:Xamarin Forms SQLite

public Task<int> InsertAsync(T entity) { 
     return _connection.InsertAsync(entity); 
    } 

此外,我创建表是这样的:

public Repository(ISQLiteConnection connectionService) { 
     _connectionService = connectionService; 
     _connection = _connectionService.GetConnection(); 
     _connection.CreateTableAsync<T>(); 
    } 

在我的网页我要插入的表:

ISQLiteConnection connectionService = DependencyService.Get<ISQLiteConnection>(); 
var activityRepo = new Repository<Activity>(connectionService); 
await activityRepo.InsertAsync(activity); 

它给我这个错误:SQLite.Net.SQLiteException:没有这样的表:活动

我把一个破发点,它的创建表和错误上弹出插入异步statment。

+0

该表也被创建为异步。你试着等待它吗?也许表格尚未创建。 – Blacktempel

+0

我想把创建表放在一个单独的函数,然后调用该函数与等待,但它给了我这个错误:System.NotSupportedException:不知道System.Collections.ObjectModel.ObservableCollection'1 [System.String ] – Mireille

回答

0

你做忽略此行的代码下的警告:

_connection.CreateTableAsync<T>(); 

警告会明确说明这个呼叫没有等待。所以,Repository的构造函数很可能会在表创建之前返回。

var repo = new Repository(connectionService); 
await repo.CreateTablesAsync(); 

,改变你的库:

public async Task CreateTablesAsync() 
{ 
    await _connection.CreateTableAsync<T>(); 
} 

不要删除任何上述await关键字或你会运行该方法,而不是等待它结束。

+0

感谢您的回复,它给我一个错误任务和函数名称,因为它应该返回 – Mireille

+0

@Mireille答案固定。请重新检查 – user3185569

+0

它给了我这个错误System.NotSupportedException:不知道System.Collections.ObjectModel.ObservableCollection'1 [System.String] – Mireille