2017-09-25 48 views
1

I have a table named profit model:我如何限制數據庫中的行數爲1?

我想活動不同的profitmodel,表中的數據將被更新。

現在我正在使用UpdateAsync,但它不工作......我該如何實現這一目標?

async void Active_Clicked(object sender, System.EventArgs e) 
    { 
     var profitmodel = (sender as Button).CommandParameter as ProfitModel; 

     await conn.CreateTableAsync<ProfitModelInUsed>(); 

     //System.Diagnostics.Debug.WriteLine(product.ProductName); 

     var profitmodelInUsed = new ProfitModelInUsed 
     { 
      ProfitModel_ID = profitmodel.ProfitModel_ID, 
      ProfitModel_Name = profitmodel.ProfitModel_Name, 
      ExchangeRate = profitmodel.ExchangeRate, 
      Profit = profitmodel.Profit 
     }; 

     await conn.UpdateAsync(profitmodelInUsed); 

     await DisplayAlert("This ProfitModel is Applied", profitmodelInUsed.ProfitModel_Name, "OK"); 
    } 

而且我不想在此表中有多行。

回答

0

套用PrimaryKeyProfitModel_ID財產的ProfitModelInUsed模型中:

public class ProfitModelInUsed 
{ 
    [PrimaryKey] 
    public string ProfitModel_ID { get; set; } 
    // ~~~ other properties 
} 

而且使用設置ProfitModel_ID屬性const

profitmodelInUsed.ProfitModel_ID = "InUseProfitModel" 
await conn.UpdateAsync(profitmodelInUsed); 

通過Retrive它:

var profitmodelinuse = conn.FindAsync<ProfitModelInUsed>("InUseProfitModel"); 
+0

謝謝兄弟!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !你的建議是有幫助的!!!!!!!!!! –