我有一个 Windows Mobile 服务,其中包含一个包含 ProductID 和 ProductName 的表。我正在使用 azure 移动服务同步服务与云同步数据。但我找不到在客户端定义表索引的方法。
public class Product
{
public string id;
[JsonProperty(PropertyName = "ProductCode")]
public int ProductCode{ get; set; }
[JsonProperty(PropertyName = "ProductName")]
public string ProductName { get; set; }
}
我将表格定义为
var store = new MobileServiceSQLiteStore("sqlite.db");
store.DefineTable<Product>();
现在如何在sqlite表中定义索引?我在 msdn 或任何其他论坛中找不到任何方法。
最佳答案
不幸的是,我没有找到任何解释这一点的文档。但我手动完成了它并且它对我有用。步骤如下:
- 首先安装此 Nuget 软件包 ( SQLite.Net PCL )。
- 创建 SQLite 表后,为所需的表和列创建索引。
代码示例
private async Task InitLocalStoreAsync()
{
var store = new MobileServiceSQLiteStore("sqlite.db");
store.DefineTable<Product>();
await App.MobileService.SyncContext.InitializeAsync(store);
// Create a new connection
using (var db = DbConnection)
{
//Adding index, say you want to add index for ProductCode column
//Add index, table and column names respectively
db.CreateIndex("IX_Product_ProductCode", "Product", "ProductCode");
}
}
private static SQLite.Net.SQLiteConnection DbConnection
{
//Here I'm using UWP, if you are using other platforms
//you have to change platform and database path.
get
{
return new SQLite.Net.SQLiteConnection(
new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(),
System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "ipglocalstore.db"));
}
}
如果您将 SQLite 与 UWP 结合使用,请查看 this article .
关于sqlite - 如何使用 Windows Azure 移动服务 SQLite 存储为 SQLite 表定义索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33466487/