2015-11-01 19 views
1

我有一個帶有包含productid和productname的表的windows移動服務。我正在使用Azure移動服務同步服務與雲同步數據。但我找不到在客戶端爲表定義索引的方法。如何使用windows Azure移動服務定義sqlite表的索引sqlite store

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或任何其他論壇。

+0

你有沒有找到答案? –

+0

你有沒有找到任何解決方案? –

回答

1

不幸的是,我還沒有找到任何文件解釋。但我手動做了它,它爲我工作。這裏是步驟:

  1. 首先安裝這個Nuget包(SQLite.Net PCL)。
  2. 創建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